{"id":1963,"date":"2023-11-29T15:34:31","date_gmt":"2023-11-29T07:34:31","guid":{"rendered":"http:\/\/zhang.mba\/?p=1963"},"modified":"2023-11-29T15:36:47","modified_gmt":"2023-11-29T07:36:47","slug":"python-gui-bian-cheng-de-ji-zhong-fang-shi","status":"publish","type":"post","link":"https:\/\/zhang.mba\/index.php\/2023\/11\/29\/15\/34\/31\/1963\/python-gui-bian-cheng-de-ji-zhong-fang-shi\/python\/zhangzhiqi\/","title":{"rendered":"Python GUI\u7f16\u7a0b\u7684\u51e0\u79cd\u65b9\u5f0f"},"content":{"rendered":"<p>\u5f15\u8a00\uff1a<\/p>\n<p>\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u662f\u73b0\u4ee3\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4e3a\u7528\u6237\u63d0\u4f9b\u4e86\u76f4\u89c2\u548c\u53cb\u597d\u7684\u4ea4\u4e92\u65b9\u5f0f\u3002Python\u4f5c\u4e3a\u4e00\u79cd\u7b80\u5355\u6613\u5b66\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u4e2a\u5e93\u548c\u5de5\u5177\uff0c\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684GUI\u5e94\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython GUI\u7f16\u7a0b\u7684\u57fa\u7840\u77e5\u8bc6\u3001\u5e38\u7528\u5e93\u548c\u5b9e\u4f8b\u6f14\u793a\u3002<\/p>\n<p>\u4e00\u3001Python GUI\u7f16\u7a0b\u57fa\u7840\u77e5\u8bc6<\/p>\n<p>1. GUI\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>GUI\u662f\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08Graphical User Interface\uff09\u7684\u7f29\u5199\uff0c\u5b83\u901a\u8fc7\u56fe\u5f62\u5316\u7684\u65b9\u5f0f\u5448\u73b0\u5e94\u7528\u7a0b\u5e8f\u7684\u754c\u9762\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u9f20\u6807\u3001\u952e\u76d8\u7b49\u8bbe\u5907\u4e0e\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<p>2. Python\u4e2d\u7684GUI\u7f16\u7a0b<\/p>\n<p>Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5e93\u548c\u5de5\u5177\uff0c\u7528\u4e8e\u521b\u5efaGUI\u5e94\u7528\u7a0b\u5e8f\u3002\u5e38\u7528\u7684GUI\u7f16\u7a0b\u5e93\u5305\u62ecTkinter\u3001PyQt\u3001wxPython\u7b49\u3002<\/p>\n<p>\u4e8c\u3001\u5e38\u7528\u7684Python GUI\u7f16\u7a0b\u5e93<\/p>\n<p>1. Tkinter<\/p>\n<p>Tkinter\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2aGUI\u7f16\u7a0b\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u521b\u5efa\u7b80\u5355\u4e14\u529f\u80fd\u5f3a\u5927\u7684GUI\u5e94\u7528\u7a0b\u5e8f\u6240\u9700\u7684\u7ec4\u4ef6\u548c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Tkinter\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7a97\u53e3\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">import tkinter as tk\n# \u521b\u5efa\u4e3b\u7a97\u53e3\nwindow = tk.Tk()\n# \u8bbe\u7f6e\u7a97\u53e3\u6807\u9898\nwindow.title(&quot;Hello World&quot;)\n# \u521b\u5efa\u6807\u7b7e\nlabel = tk.Label(window, text=&quot;Hello, World!&quot;)\nlabel.pack()\n# \u8fd0\u884c\u7a97\u53e3\u4e3b\u5faa\u73af\nwindow.mainloop()\n<\/code><\/pre>\n<p>2. PyQt<\/p>\n<p>PyQt\u662f\u4e00\u4e2aPython\u7ed1\u5b9a\u7684Qt\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u521b\u5efa\u8de8\u5e73\u53f0GUI\u5e94\u7528\u7a0b\u5e8f\u7684\u4e30\u5bcc\u7ec4\u4ef6\u548c\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PyQt\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7a97\u53e3\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QLabel, QWidget\n# \u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61\napp = QApplication([])\n# \u521b\u5efa\u7a97\u53e3\u5bf9\u8c61\nwindow = QWidget()\n# \u8bbe\u7f6e\u7a97\u53e3\u6807\u9898\nwindow.setWindowTitle(&quot;Hello World&quot;)\n# \u521b\u5efa\u6807\u7b7e\nlabel = QLabel(&quot;Hello, World!&quot;, parent=window)\nlabel.move(20, 20)\n# \u663e\u793a\u7a97\u53e3\nwindow.show()\n# \u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\napp.exec_()\n<\/code><\/pre>\n<p>3. wxPython<\/p>\n<p>wxPython\u662f\u4e00\u4e2a\u57fa\u4e8ewxWidgets\u7684Python GUI\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u521b\u5efa\u8de8\u5e73\u53f0GUI\u5e94\u7528\u7a0b\u5e8f\u7684\u4e30\u5bcc\u7ec4\u4ef6\u548c\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528wxPython\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7a97\u53e3\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">import wx\n# \u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61\napp = wx.App()\n# \u521b\u5efa\u7a97\u53e3\u5bf9\u8c61\nframe = wx.Frame(None, title=&quot;Hello World&quot;)\n# \u521b\u5efa\u6807\u7b7e\nlabel = wx.StaticText(frame, label=&quot;Hello, World!&quot;)\n# \u663e\u793a\u7a97\u53e3\nframe.Show()\n# \u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\napp.MainLoop()\n<\/code><\/pre>\n<p>\u4e09\u3001Python GUI\u7f16\u7a0b\u793a\u4f8b\u6f14\u793a<\/p>\n<p>1. \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u5668\u5e94\u7528\u7a0b\u5e8f\uff0c\u5305\u62ec\u6570\u5b57\u6309\u94ae\u3001\u8fd0\u7b97\u7b26\u6309\u94ae\u548c\u663e\u793a\u7ed3\u679c\u7684\u6587\u672c\u6846\u3002<\/p>\n<p>2. \u521b\u5efa\u4e00\u4e2a\u56fe\u50cf\u67e5\u770b\u5668\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u6253\u5f00\u548c\u663e\u793a\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u63d0\u4f9b\u7f29\u653e\u3001\u65cb\u8f6c\u7b49\u529f\u80fd\u3002<\/p>\n<p>3. \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5f85\u529e\u4e8b\u9879\u5217\u8868\u5e94\u7528\u7a0b\u5e8f\uff0c\u5305\u62ec\u6dfb\u52a0\u3001\u5220\u9664\u548c\u5b8c\u6210\u5f85\u529e\u4e8b\u9879\u7684\u529f\u80fd\u3002<\/p>\n<p>\u7ed3\u8bba\uff1a<\/p>\n<p>Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5f3a\u5927\u7684GUI\u7f16\u7a0b\u5e93\uff0c\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u4ea4\u4e92\u5f0f\u5e94\u7528\u7a0b\u5e8f\u3002Tkinter\u662f\u6700\u5e38\u7528\u7684GUI\u5e93\uff0c\u9002\u5408\u5feb\u901f\u521b\u5efa\u7b80\u5355\u7684\u754c\u9762\u3002PyQt\u548cwxPython\u5219\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u7ec4\u4ef6\u548c\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u66f4\u590d\u6742\u548c\u8de8\u5e73\u53f0\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u638c\u63e1\u8fd9\u4e9b\u5e93\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6253\u9020\u51fa\u4ee4\u4eba\u5370\u8c61\u6df1\u523b\u7684GUI\u5e94\u7528\u7a0b\u5e8f\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002\u65e0\u8bba\u662f\u521d\u5b66\u8005\u8fd8\u662f\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\uff0c\u90fd\u53ef\u4ee5\u5229\u7528Python\u7684\u5f3a\u5927\u529f\u80fd\u548c\u4e30\u5bcc\u7684GUI\u5e93\uff0c\u8f7b\u677e\u6784\u5efa\u51fa\u4ea4\u4e92\u5f0f\u5e94\u7528\u7684\u5b8c\u7f8e\u5de5\u5177\u3002<\/p>\n<p>\u6765\u6e90&#8212;&#8211;Python\u5c0f\u8d34\u58eb<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00\uff1a \u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u662f\u73b0\u4ee3\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4e3a\u7528\u6237\u63d0\u4f9b\u4e86\u76f4\u89c2\u548c\u53cb\u597d\u7684\u4ea4\u4e92\u65b9\u5f0f\u3002Python\u4f5c\u4e3a\u4e00\u79cd\u7b80\u5355\u6613\u5b66\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u4e2a\u5e93\u548c\u5de5\u5177\uff0c\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684GUI\u5e94\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython GUI\u7f16\u7a0b\u7684\u57fa\u7840\u77e5\u8bc6\u3001\u5e38\u7528 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[58,12],"tags":[],"_links":{"self":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1963"}],"collection":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/comments?post=1963"}],"version-history":[{"count":0,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1963\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/media?parent=1963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/categories?post=1963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/tags?post=1963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}