{"id":1169972,"date":"2025-01-15T16:16:53","date_gmt":"2025-01-15T08:16:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1169972.html"},"modified":"2025-01-15T16:16:56","modified_gmt":"2025-01-15T08:16:56","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5wx%e5%ba%93%e5%ae%89%e8%a3%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1169972.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165wx\u5e93\u5b89\u88c5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070804\/790fc34c-5cb5-477f-ab41-b5163f12a56c.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165wx\u5e93\u5b89\u88c5\" \/><\/p>\n<p><p> <strong>Python\u5bfc\u5165wx\u5e93\u5b89\u88c5\u6b65\u9aa4\uff1a<\/strong><\/p>\n<\/p>\n<p><p><strong>1\u3001\u5b89\u88c5wxPython\u5e93<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528pip\u6765\u5b89\u88c5wxPython\u5e93\u3002\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install wxPython<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6267\u884c\u6b64\u547d\u4ee4\u540e\uff0cpip\u5c06\u81ea\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5wxPython\u5e93\u3002<strong>\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86pip<\/strong>\uff0c\u5e76\u4e14Python\u7248\u672c\u57283.5\u53ca\u4ee5\u4e0a\uff0c\u56e0\u4e3awxPython\u7684\u65b0\u7248\u672c\u53ea\u652f\u6301\u8fd9\u4e9b\u7248\u672c\u7684Python\u3002<\/p>\n<\/p>\n<p><p><strong>2\u3001\u5bfc\u5165wxPython\u5e93<\/strong><\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165wxPython\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<p>app = wx.App(False)<\/p>\n<p>frame = wx.Frame(None, wx.ID_ANY, &quot;Hello, wxPython!&quot;)<\/p>\n<p>frame.Show(True)<\/p>\n<p>app.M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86wx\u6a21\u5757\uff0c\u521b\u5efa\u4e86\u4e00\u4e2awx.App\u5bf9\u8c61\u6765\u521d\u59cb\u5316\u5e94\u7528\u7a0b\u5e8f\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u6846\u67b6\u7a97\u53e3(wx.Frame)\uff0c\u5e76\u663e\u793a\u5b83\u3002\u6700\u540e\uff0c\u6211\u4eec\u8fdb\u5165\u5e94\u7528\u7a0b\u5e8f\u7684\u4e8b\u4ef6\u5faa\u73af(MainLoop)\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u786e\u4fddpip\u548cPython\u7248\u672c\u5339\u914d<\/strong><\/p>\n<p>\u5728\u5b89\u88c5wxPython\u5e93\u4e4b\u524d\uff0c<strong>\u786e\u4fdd\u4f60\u7684pip\u548cPython\u7248\u672c\u5339\u914d<\/strong>\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u68c0\u67e5\u4f60\u7684Python\u548cpip\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p>pip --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86\u591a\u4e2a\u7248\u672c\u7684Python\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7248\u672c\u7684pip\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8ePython 3\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528pip3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip3 install wxPython<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fddwxPython\u5e93\u5b89\u88c5\u5728\u6b63\u786e\u7684Python\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<hr>\n<p><p><strong>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b89\u88c5wxPython\u4e4b\u524d\uff0c\u6709\u4e00\u4e9b\u51c6\u5907\u5de5\u4f5c\u662f\u5fc5\u8981\u7684\u3002\u8fd9\u4e9b\u51c6\u5907\u5de5\u4f5c\u786e\u4fdd\u5b89\u88c5\u8fc7\u7a0b\u7684\u987a\u5229\u8fdb\u884c\uff0c\u5e76\u4e14\u907f\u514d\u4e00\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p><strong>1.1\u3001\u786e\u4fddPython\u5df2\u5b89\u88c5<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5728\u4f60\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86Python\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5Python\u662f\u5426\u5df2\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u5b89\u88c5Python\uff0c\u53ef\u4ee5\u4ecePython\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u3002<\/p>\n<\/p>\n<p><p><strong>1.2\u3001\u5b89\u88c5pip<\/strong><\/p>\n<\/p>\n<p><p>pip\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305\u3002\u5927\u591a\u6570\u73b0\u4ee3\u7248\u672c\u7684Python\u90fd\u4f1a\u81ea\u5e26pip\uff0c\u4f46\u5982\u679c\u4f60\u7684Python\u5b89\u88c5\u6ca1\u6709pip\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5pip\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">curl https:\/\/bootstrap.pypa.io\/get-pip.py -o get-pip.py<\/p>\n<p>python get-pip.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u5b89\u88c5wxPython<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u65e6\u4f60\u786e\u8ba4\u4f60\u7684\u7cfb\u7edf\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u548cpip\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u5b89\u88c5wxPython\u5e93\u3002<\/p>\n<\/p>\n<p><p><strong>2.1\u3001\u4f7f\u7528pip\u5b89\u88c5wxPython<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5wxPython\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install wxPython<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684wxPython\u3002<\/p>\n<\/p>\n<p><p><strong>2.2\u3001\u9a8c\u8bc1\u5b89\u88c5<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fddwxPython\u5e93\u5b89\u88c5\u6210\u529f\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c\u5e76\u8fd0\u884c\u5b83\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<p>app = wx.App(False)<\/p>\n<p>frame = wx.Frame(None, wx.ID_ANY, &quot;Hello, wxPython!&quot;)<\/p>\n<p>frame.Show(True)<\/p>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fdd\u5b58\u6587\u4ef6\u5e76\u8fd0\u884c\u5b83\u3002\u5982\u679c\u4e00\u4e2a\u7a97\u53e3\u5f39\u51fa\u5e76\u663e\u793a\u201cHello, wxPython!\u201d\uff0c\u90a3\u4e48\u8bf4\u660ewxPython\u5df2\u7ecf\u6210\u529f\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b89\u88c5wxPython\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>3.1\u3001pip\u7248\u672c\u8fc7\u65e7<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684pip\u7248\u672c\u8fc7\u65e7\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b89\u88c5\u5931\u8d25\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5347\u7ea7pip\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install --upgrade pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3.2\u3001\u7f51\u7edc\u95ee\u9898<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u7531\u4e8e\u7f51\u7edc\u95ee\u9898\uff0cpip\u53ef\u80fd\u65e0\u6cd5\u4e0b\u8f7dwxPython\u5305\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u56fd\u5185\u7684\u955c\u50cf\u6e90\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple wxPython<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3.3\u3001\u4f9d\u8d56\u95ee\u9898<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u4e9b\u7cfb\u7edf\u53ef\u80fd\u7f3a\u5c11\u4e00\u4e9bwxPython\u4f9d\u8d56\u7684\u7cfb\u7edf\u5e93\u3002\u4f60\u53ef\u4ee5\u53c2\u8003wxPython\u7684\u5b98\u65b9\u7f51\u7ad9\u6216\u6587\u6863\uff0c\u67e5\u627e\u4f60\u6240\u5728\u7cfb\u7edf\u7684\u4f9d\u8d56\u5e93\u5e76\u5b89\u88c5\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001wxPython\u7684\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5b66\u4e60\u5982\u4f55\u4f7f\u7528wxPython\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4e0b\u9762\u4ecb\u7ecd\u4e00\u4e9bwxPython\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>4.1\u3001\u521b\u5efa\u7b80\u5355\u7684GUI<\/strong><\/p>\n<\/p>\n<p><p>wxPython\u662f\u4e00\u4e2a\u5f3a\u5927\u7684GUI\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u7a97\u53e3\u5e94\u7528\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa\u7b80\u5355\u7a97\u53e3\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<p>class MyFrame(wx.Frame):<\/p>\n<p>    def __init__(self, *args, kw):<\/p>\n<p>        super(MyFrame, self).__init__(*args, kw)<\/p>\n<p>        pnl = wx.Panel(self)<\/p>\n<p>        st = wx.StaticText(pnl, label=&quot;Hello, wxPython!&quot;, pos=(25,25))<\/p>\n<p>app = wx.App(False)<\/p>\n<p>frm = MyFrame(None, title=&quot;Simple wxPython App&quot;, size=(300,200))<\/p>\n<p>frm.Show()<\/p>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aMyFrame\u7c7b\uff0c\u5b83\u7ee7\u627f\u81eawx.Frame\u3002\u6211\u4eec\u5728\u7a97\u53e3\u4e0a\u6dfb\u52a0\u4e86\u4e00\u4e2a\u9759\u6001\u6587\u672c(wx.StaticText)\uff0c\u5e76\u663e\u793a\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><p><strong>4.2\u3001\u4e8b\u4ef6\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u4e8b\u4ef6\u5904\u7406\u662fGUI\u7f16\u7a0b\u7684\u91cd\u8981\u90e8\u5206\uff0cwxPython\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u4e8b\u4ef6\u5904\u7406\u673a\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<p>class MyFrame(wx.Frame):<\/p>\n<p>    def __init__(self, *args, kw):<\/p>\n<p>        super(MyFrame, self).__init__(*args, kw)<\/p>\n<p>        pnl = wx.Panel(self)<\/p>\n<p>        self.btn = wx.Button(pnl, label=&quot;Click Me&quot;, pos=(25,25))<\/p>\n<p>        self.btn.Bind(wx.EVT_BUTTON, self.OnButtonClicked)<\/p>\n<p>    def OnButtonClicked(self, event):<\/p>\n<p>        wx.MessageBox(&quot;Button clicked!&quot;, &quot;Info&quot;, wx.OK | wx.ICON_INFORMATION)<\/p>\n<p>app = wx.App(False)<\/p>\n<p>frm = MyFrame(None, title=&quot;Event Handling&quot;, size=(300,200))<\/p>\n<p>frm.Show()<\/p>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6309\u94ae(wx.Button)\uff0c\u5e76\u7ed1\u5b9a\u4e86\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570(self.OnButtonClicked)\u3002\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c\u5f39\u51fa\u4e00\u4e2a\u6d88\u606f\u6846(wx.MessageBox)\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001wxPython\u9ad8\u7ea7\u529f\u80fd<\/strong><\/p>\n<\/p>\n<p><p>wxPython\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u57fa\u672c\u7684GUI\u7ec4\u4ef6\uff0c\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\u7684\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><p><strong>5.1\u3001\u5e03\u5c40\u7ba1\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5e03\u5c40\u7ba1\u7406\u662fGUI\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\u3002wxPython\u63d0\u4f9b\u4e86\u591a\u79cd\u5e03\u5c40\u7ba1\u7406\u5668\uff0c\u5982wx.BoxSizer\u3001wx.GridSizer\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528wx.BoxSizer\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<p>class MyFrame(wx.Frame):<\/p>\n<p>    def __init__(self, *args, kw):<\/p>\n<p>        super(MyFrame, self).__init__(*args, kw)<\/p>\n<p>        pnl = wx.Panel(self)<\/p>\n<p>        sizer = wx.BoxSizer(wx.VERTICAL)<\/p>\n<p>        self.btn1 = wx.Button(pnl, label=&quot;Button 1&quot;)<\/p>\n<p>        self.btn2 = wx.Button(pnl, label=&quot;Button 2&quot;)<\/p>\n<p>        sizer.Add(self.btn1, 0, wx.ALL, 5)<\/p>\n<p>        sizer.Add(self.btn2, 0, wx.ALL, 5)<\/p>\n<p>        pnl.SetSizer(sizer)<\/p>\n<p>app = wx.App(False)<\/p>\n<p>frm = MyFrame(None, title=&quot;Layout Management&quot;, size=(300,200))<\/p>\n<p>frm.Show()<\/p>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5782\u76f4\u5e03\u5c40\u7ba1\u7406\u5668(wx.BoxSizer(wx.VERTICAL))\uff0c\u5e76\u5c06\u4e24\u4e2a\u6309\u94ae\u6dfb\u52a0\u5230\u5e03\u5c40\u7ba1\u7406\u5668\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>5.2\u3001\u7ed8\u56fe\u529f\u80fd<\/strong><\/p>\n<\/p>\n<p><p>wxPython\u8fd8\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7ed8\u56fe\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<p>class MyFrame(wx.Frame):<\/p>\n<p>    def __init__(self, *args, kw):<\/p>\n<p>        super(MyFrame, self).__init__(*args, kw)<\/p>\n<p>        pnl = wx.Panel(self)<\/p>\n<p>        pnl.Bind(wx.EVT_PAINT, self.OnPaint)<\/p>\n<p>    def OnPaint(self, event):<\/p>\n<p>        dc = wx.PaintDC(self)<\/p>\n<p>        dc.SetBrush(wx.Brush(wx.Colour(255, 0, 0)))<\/p>\n<p>        dc.DrawCircle(50, 50, 25)<\/p>\n<p>app = wx.App(False)<\/p>\n<p>frm = MyFrame(None, title=&quot;Drawing&quot;, size=(300,200))<\/p>\n<p>frm.Show()<\/p>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u9762\u677f\u4e0a\u7ed1\u5b9a\u4e86\u4e00\u4e2a\u7ed8\u5236\u4e8b\u4ef6(wx.EVT_PAINT)\uff0c\u5e76\u5728\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\u7ed8\u5236\u4e86\u4e00\u4e2a\u7ea2\u8272\u7684\u5706\u3002<\/p>\n<\/p>\n<p><p><strong>\u516d\u3001wxPython\u7684\u6269\u5c55\u548c\u7b2c\u4e09\u65b9\u5e93<\/strong><\/p>\n<\/p>\n<p><p>wxPython\u4e0d\u4ec5\u81ea\u8eab\u529f\u80fd\u5f3a\u5927\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6269\u5c55\u548c\u7b2c\u4e09\u65b9\u5e93\u6765\u589e\u5f3a\u5176\u529f\u80fd\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u7528\u7684\u6269\u5c55\u548c\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<\/p>\n<p><p><strong>6.1\u3001wx.lib<\/strong><\/p>\n<\/p>\n<p><p>wx.lib\u662fwxPython\u7684\u6269\u5c55\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u989d\u5916\u7684\u7ec4\u4ef6\u548c\u529f\u80fd\u3002\u4f8b\u5982\uff0cwx.lib.plot\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u56fe\u8868\uff0cwx.lib.agw\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u5c0f\u90e8\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528wx.lib.plot\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<p>import wx.lib.plot as plot<\/p>\n<p>class MyFrame(wx.Frame):<\/p>\n<p>    def __init__(self, *args, kw):<\/p>\n<p>        super(MyFrame, self).__init__(*args, kw)<\/p>\n<p>        pnl = wx.Panel(self)<\/p>\n<p>        data = [(0, 0), (1, 1), (2, 4), (3, 9)]<\/p>\n<p>        line = plot.PolyLine(data, colour=&#39;blue&#39;, width=1)<\/p>\n<p>        gc = plot.PlotGraphics([line], &#39;Demo Plot&#39;, &#39;X Axis&#39;, &#39;Y Axis&#39;)<\/p>\n<p>        self.plot = plot.PlotCanvas(pnl)<\/p>\n<p>        self.plot.Draw(gc, xAxis=(0, 3), yAxis=(0, 10))<\/p>\n<p>app = wx.App(False)<\/p>\n<p>frm = MyFrame(None, title=&quot;Plotting&quot;, size=(300,200))<\/p>\n<p>frm.Show()<\/p>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528wx.lib.plot.PlotCanvas\u7ed8\u5236\u4e86\u4e00\u6761\u7b80\u5355\u7684\u6298\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<p><p><strong>6.2\u3001\u7b2c\u4e09\u65b9\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86wx.lib\uff0c\u793e\u533a\u8fd8\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u4e0ewxPython\u4e00\u8d77\u4f7f\u7528\u3002\u4f8b\u5982\uff0cmatplotlib\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u590d\u6742\u7684\u56fe\u8868\uff0cpillow\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528matplotlib\u4e0ewxPython\u96c6\u6210\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<p>import matplotlib<\/p>\n<p>matplotlib.use(&#39;WXAgg&#39;)<\/p>\n<p>from matplotlib.figure import Figure<\/p>\n<p>from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas<\/p>\n<p>class MyFrame(wx.Frame):<\/p>\n<p>    def __init__(self, *args, kw):<\/p>\n<p>        super(MyFrame, self).__init__(*args, kw)<\/p>\n<p>        pnl = wx.Panel(self)<\/p>\n<p>        sizer = wx.BoxSizer(wx.VERTICAL)<\/p>\n<p>        self.figure = Figure()<\/p>\n<p>        self.axes = self.figure.add_subplot(111)<\/p>\n<p>        self.canvas = FigureCanvas(pnl, -1, self.figure)<\/p>\n<p>        sizer.Add(self.canvas, 1, wx.EXPAND)<\/p>\n<p>        pnl.SetSizer(sizer)<\/p>\n<p>        self.plot()<\/p>\n<p>    def plot(self):<\/p>\n<p>        x = [0, 1, 2, 3]<\/p>\n<p>        y = [0, 1, 4, 9]<\/p>\n<p>        self.axes.plot(x, y)<\/p>\n<p>        self.canvas.draw()<\/p>\n<p>app = wx.App(False)<\/p>\n<p>frm = MyFrame(None, title=&quot;Matplotlib with wxPython&quot;, size=(300,200))<\/p>\n<p>frm.Show()<\/p>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2amatplotlib\u56fe\u8868\uff0c\u5e76\u5c06\u5176\u5d4c\u5165\u5230wxPython\u7a97\u53e3\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e03\u3001wxPython\u7684\u90e8\u7f72<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u4f60\u7684wxPython\u5e94\u7528\u7a0b\u5e8f\u90e8\u7f72\u5230\u5176\u4ed6\u8ba1\u7b97\u673a\u4e0a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u90e8\u7f72\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>7.1\u3001\u6253\u5305\u4e3a\u72ec\u7acb\u5e94\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528pyinstaller\u6216cx_Freeze\u7b49\u5de5\u5177\u5c06\u4f60\u7684wxPython\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u4e3a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528pyinstaller\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p>pyinstaller --onefile myscript.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u751f\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5305\u542b\u6240\u6709\u5fc5\u8981\u7684\u4f9d\u8d56\u5e93\u3002<\/p>\n<\/p>\n<p><p><strong>7.2\u3001\u521b\u5efa\u5b89\u88c5\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528Inno Setup\u7b49\u5de5\u5177\u521b\u5efa\u5b89\u88c5\u5305\uff0c\u65b9\u4fbf\u7528\u6237\u5b89\u88c5\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u3002Inno Setup\u662f\u4e00\u4e2a\u514d\u8d39\u7684\u5b89\u88c5\u5305\u5236\u4f5c\u5de5\u5177\uff0c\u652f\u6301\u591a\u79cd\u5b89\u88c5\u9009\u9879\u548c\u81ea\u5b9a\u4e49\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><p><strong>\u516b\u3001wxPython\u7684\u793e\u533a\u548c\u8d44\u6e90<\/strong><\/p>\n<\/p>\n<p><p>wxPython\u6709\u4e00\u4e2a\u6d3b\u8dc3\u7684\u793e\u533a\u548c\u4e30\u5bcc\u7684\u8d44\u6e90\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5b66\u4e60\u548c\u4f7f\u7528wxPython\u3002<\/p>\n<\/p>\n<p><p><strong>8.1\u3001\u5b98\u65b9\u6587\u6863<\/strong><\/p>\n<\/p>\n<p><p>wxPython\u7684\u5b98\u65b9\u6587\u6863\u662f\u6700\u6743\u5a01\u7684\u8d44\u6e90\uff0c\u5305\u542b\u4e86\u8be6\u7ec6\u7684API\u6587\u6863\u548c\u4f7f\u7528\u793a\u4f8b\u3002\u4f60\u53ef\u4ee5\u5728wxPython\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0a\u627e\u5230\u5b98\u65b9\u6587\u6863\u3002<\/p>\n<\/p>\n<p><p><strong>8.2\u3001\u793e\u533a\u8bba\u575b<\/strong><\/p>\n<\/p>\n<p><p>wxPython\u6709\u4e00\u4e2a\u6d3b\u8dc3\u7684\u793e\u533a\u8bba\u575b\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u8bba\u575b\u4e0a\u63d0\u95ee\u3001\u5206\u4eab\u7ecf\u9a8c\u548c\u8ba8\u8bba\u95ee\u9898\u3002\u4f60\u53ef\u4ee5\u5728wxPython\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0a\u627e\u5230\u793e\u533a\u8bba\u575b\u7684\u94fe\u63a5\u3002<\/p>\n<\/p>\n<p><p><strong>8.3\u3001\u6559\u7a0b\u548c\u4e66\u7c4d<\/strong><\/p>\n<\/p>\n<p><p>\u7f51\u4e0a\u6709\u8bb8\u591a\u5173\u4e8ewxPython\u7684\u6559\u7a0b\u548c\u4e66\u7c4d\uff0c\u9002\u5408\u4e0d\u540c\u6c34\u5e73\u7684\u7528\u6237\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u641c\u7d22\u5f15\u64ce\u67e5\u627e\u76f8\u5173\u7684\u6559\u7a0b\u548c\u4e66\u7c4d\uff0c\u6216\u8005\u5728\u7f16\u7a0b\u4e66\u5e97\u4e2d\u5bfb\u627e\u9002\u5408\u4f60\u7684\u4e66\u7c4d\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u8d44\u6e90\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u987a\u5229\u5b89\u88c5\u548c\u4f7f\u7528wxPython\u5e93\uff0c\u5e76\u5f00\u53d1\u51fa\u529f\u80fd\u4e30\u5bcc\u7684GUI\u5e94\u7528\u7a0b\u5e8f\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5wxPython\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528wxPython\u5e93\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u8fdb\u884c\u5b89\u88c5\u3002\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>pip install wxPython\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u4ecePython\u7684\u5305\u7d22\u5f15\u4e2d\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684wxPython\u3002<\/p>\n<p><strong>wxPython\u4e0e\u5176\u4ed6GUI\u5e93\u76f8\u6bd4\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>wxPython\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684GUI\u6846\u67b6\uff0c\u4e0eTkinter\u548cPyQt\u7b49\u5176\u4ed6\u5e93\u76f8\u6bd4\uff0cwxPython\u63d0\u4f9b\u4e86\u66f4\u4e3a\u4e30\u5bcc\u7684\u672c\u5730\u5316\u754c\u9762\u3002\u5b83\u80fd\u591f\u521b\u5efa\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u539f\u751f\u5916\u89c2\u4e00\u81f4\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u9002\u5408\u9700\u8981\u590d\u6742\u7528\u6237\u754c\u9762\u7684\u9879\u76ee\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528wxPython\u65f6\u5e38\u89c1\u7684\u9519\u8bef\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u4f7f\u7528wxPython\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u7248\u672c\u4e0d\u517c\u5bb9\u3001\u7f3a\u5c11\u4f9d\u8d56\u9879\u4ee5\u53ca\u5728\u4e0d\u652f\u6301\u7684\u73af\u5883\u4e2d\u8fd0\u884c\u3002\u786e\u4fdd\u60a8\u7684Python\u7248\u672c\u4e0ewxPython\u517c\u5bb9\uff0c\u5e76\u4ed4\u7ec6\u9605\u8bfb\u9519\u8bef\u4fe1\u606f\uff0c\u4ee5\u627e\u51fa\u5177\u4f53\u95ee\u9898\u5e76\u8fdb\u884c\u4fee\u590d\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u4f9d\u8d56\u95ee\u9898\uff0c\u53ef\u4ee5\u67e5\u9605wxPython\u7684\u5b98\u65b9\u6587\u6863\u6216\u793e\u533a\u8bba\u575b\u4ee5\u83b7\u5f97\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5bfc\u5165wx\u5e93\u5b89\u88c5\u6b65\u9aa4\uff1a 1\u3001\u5b89\u88c5wxPython\u5e93 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528pip\u6765\u5b89\u88c5wxPython\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1169980,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169972"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1169972"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169972\/revisions"}],"predecessor-version":[{"id":1169981,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169972\/revisions\/1169981"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1169980"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1169972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1169972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1169972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}