{"id":985720,"date":"2024-12-27T07:39:05","date_gmt":"2024-12-26T23:39:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/985720.html"},"modified":"2024-12-27T07:39:07","modified_gmt":"2024-12-26T23:39:07","slug":"%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80python%e7%9a%84gui","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/985720.html","title":{"rendered":"\u5982\u4f55\u6253\u5f00python\u7684gui"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25062637\/8625dc31-ba75-4207-a5e5-1f6faf5b0798.webp\" alt=\"\u5982\u4f55\u6253\u5f00python\u7684gui\" \/><\/p>\n<p><p> <strong>\u8981\u6253\u5f00Python\u7684GUI\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5de5\u5177\u548c\u5e93\uff0c\u5305\u62ecTkinter\u3001PyQt\u3001wxPython\u7b49\u3002<\/strong>\u4f7f\u7528Tkinter\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002Tkinter\u7b80\u5355\u6613\u5b66\uff0c\u9002\u5408\u521d\u5b66\u8005\uff1bPyQt\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u9700\u8981\u590d\u6742\u754c\u9762\u7684\u5e94\u7528\uff1bwxPython\u5219\u63d0\u4f9b\u4e86\u672c\u5730\u5316\u7684\u5916\u89c2\u548c\u611f\u89c9\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tkinter\u6253\u5f00\u4e00\u4e2a\u7b80\u5355\u7684Python GUI\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Tkinter\u521b\u5efaGUI\u5e94\u7528\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a<strong>\u5bfc\u5165Tkinter\u5e93\u3001\u521b\u5efa\u4e3b\u7a97\u53e3\u3001\u6dfb\u52a0\u63a7\u4ef6\uff08\u5982\u6309\u94ae\u3001\u6807\u7b7e\uff09\u3001\u8bbe\u7f6e\u5e03\u5c40\u7ba1\u7406\u5668\u3001\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong>\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python\uff0c\u56e0\u4e3aTkinter\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u901a\u5e38\u9ed8\u8ba4\u5b89\u88c5\u3002\u7136\u540e\uff0c\u6253\u5f00\u4f60\u7684\u4ee3\u7801\u7f16\u8f91\u5668\u6216Python\u7ec8\u7aef\uff0c\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Tkinter\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165Tkinter\u5e93<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u4f7f\u7528\u5b83\u4e4b\u524d\u9700\u8981\u5bfc\u5165\u3002\u901a\u5e38\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u5bfc\u5165Tkinter\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u5bfc\u5165\u4e86Tkinter\u6a21\u5757\uff0c\u5e76\u5c06\u5176\u91cd\u547d\u540d\u4e3a<code>tk<\/code>\uff0c\u8fd9\u6837\u5728\u4f7f\u7528\u65f6\u4f1a\u66f4\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u4e3b\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e3b\u7a97\u53e3\u662fGUI\u5e94\u7528\u7684\u57fa\u7840\u3002\u53ef\u4ee5\u901a\u8fc7\u5b9e\u4f8b\u5316<code>Tk<\/code>\u7c7b\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root = tk.Tk()<\/p>\n<p>root.title(&quot;My First GUI&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>root<\/code>\u7684\u4e3b\u7a97\u53e3\uff0c\u8bbe\u7f6e\u4e86\u7a97\u53e3\u7684\u6807\u9898\u4e3a&quot;My First GUI&quot;\uff0c\u5e76\u5c06\u7a97\u53e3\u5927\u5c0f\u8bbe\u7f6e\u4e3a300&#215;200\u50cf\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6dfb\u52a0\u63a7\u4ef6<\/h3>\n<\/p>\n<p><p>\u63a7\u4ef6\u662f\u7528\u6237\u4e0e\u5e94\u7528\u7a0b\u5e8f\u4ea4\u4e92\u7684\u5143\u7d20\uff0c\u5982\u6309\u94ae\u3001\u6807\u7b7e\u3001\u6587\u672c\u6846\u7b49\u3002\u4e0b\u9762\u662f\u5982\u4f55\u5728\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u7b80\u5355\u7684\u6807\u7b7e\u548c\u6309\u94ae\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">label = tk.Label(root, text=&quot;Hello, World!&quot;)<\/p>\n<p>label.pack()<\/p>\n<p>button = tk.Button(root, text=&quot;Click Me&quot;, command=root.quit)<\/p>\n<p>button.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>Label<\/code>\u662f\u4e00\u4e2a\u6587\u672c\u63a7\u4ef6\uff0c\u7528\u4e8e\u663e\u793a\u9759\u6001\u6587\u672c\u3002<code>Button<\/code>\u662f\u4e00\u4e2a\u6309\u94ae\u63a7\u4ef6\uff0c<code>command<\/code>\u53c2\u6570\u6307\u5b9a\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\u8c03\u7528\u7684\u51fd\u6570\uff0c\u8fd9\u91cc\u4f7f\u7528<code>root.quit<\/code>\u6765\u5173\u95ed\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8bbe\u7f6e\u5e03\u5c40\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u5e03\u5c40\u7ba1\u7406\u5668\u7528\u4e8e\u63a7\u5236\u63a7\u4ef6\u5728\u7a97\u53e3\u4e2d\u7684\u6392\u5217\u3002Tkinter\u63d0\u4f9b\u4e86\u4e09\u79cd\u5e03\u5c40\u7ba1\u7406\u5668\uff1apack\u3001grid\u548cplace\u3002\u4e0a\u9762\u7684\u793a\u4f8b\u4f7f\u7528\u4e86<code>pack<\/code>\u5e03\u5c40\u7ba1\u7406\u5668\uff0c\u5b83\u4ee5\u5806\u53e0\u7684\u65b9\u5f0f\u6392\u5217\u63a7\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8fd0\u884c\u4e3b\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u4e3b\u5faa\u73af\u662fGUI\u5e94\u7528\u7684\u6838\u5fc3\uff0c\u5b83\u8d1f\u8d23\u76d1\u542c\u7528\u6237\u4e8b\u4ef6\u5e76\u66f4\u65b0\u754c\u9762\u3002\u901a\u8fc7\u8c03\u7528<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/code>\u65b9\u6cd5\u542f\u52a8\u4e3b\u5faa\u73af\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u4f7f\u5e94\u7528\u7a0b\u5e8f\u4fdd\u6301\u8fd0\u884c\u72b6\u6001\uff0c\u76f4\u5230\u7528\u6237\u5173\u95ed\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528PyQt\u548cwxPython<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Tkinter\u5916\uff0cPyQt\u548cwxPython\u4e5f\u662f\u521b\u5efaPython GUI\u5e94\u7528\u7684\u6d41\u884c\u9009\u62e9\u3002PyQt\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u5f3a\u5927\u7684\u5e03\u5c40\u7ba1\u7406\u5668\uff0c\u9002\u5408\u9700\u8981\u590d\u6742\u754c\u9762\u7684\u5e94\u7528\u3002wxPython\u5219\u63d0\u4f9b\u4e86\u672c\u5730\u5316\u7684\u5916\u89c2\u548c\u611f\u89c9\uff0c\u9002\u5408\u9700\u8981\u8de8\u5e73\u53f0\u4e00\u81f4\u6027\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528PyQt\u521b\u5efaGUI<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5PyQt\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyQt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528PyQt\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7a97\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QLabel, QWidget<\/p>\n<p>app = QApplication([])<\/p>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&quot;PyQt5 GUI&quot;)<\/p>\n<p>layout = QVBoxLayout()<\/p>\n<p>layout.addWidget(QLabel(&#39;Hello, PyQt5!&#39;))<\/p>\n<p>window.setLayout(layout)<\/p>\n<p>window.show()<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528wxPython\u521b\u5efaGUI<\/h4>\n<\/p>\n<p><p>\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>\u4f7f\u7528wxPython\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7a97\u53e3\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;wxPython GUI&quot;)<\/p>\n<p>panel = wx.Panel(frame)<\/p>\n<p>label = wx.StaticText(panel, label=&quot;Hello, wxPython!&quot;, pos=(10,10))<\/p>\n<p>frame.Show(True)<\/p>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u4e2a\u5e93\uff0c\u521b\u5efaPython GUI\u5e94\u7528\u7684\u57fa\u672c\u6b65\u9aa4\u90fd\u662f\u7c7b\u4f3c\u7684\uff1a<strong>\u5bfc\u5165\u5e93\u3001\u521b\u5efa\u4e3b\u7a97\u53e3\u3001\u6dfb\u52a0\u63a7\u4ef6\u3001\u8bbe\u7f6e\u5e03\u5c40\u3001\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong>\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u5e94\u7528\u7684\u590d\u6742\u6027\u548c\u9700\u8981\u7684\u529f\u80fd\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5e94\u7528\uff0cTkinter\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u5bf9\u4e8e\u9700\u8981\u66f4\u591a\u529f\u80fd\u548c\u590d\u6742\u5e03\u5c40\u7684\u5e94\u7528\uff0cPyQt\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff1b\u800cwxPython\u5219\u9002\u5408\u9700\u8981\u8de8\u5e73\u53f0\u4e00\u81f4\u6027\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u6253\u5f00\u5e76\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Python GUI\u5e94\u7528\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5e2e\u52a9\u4f60\u5728Python GUI\u5f00\u53d1\u7684\u65c5\u7a0b\u4e2d\u8d70\u5f97\u66f4\u8fdc\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u9002\u5408\u6211\u7684\u9879\u76ee\u7684Python GUI\u5e93\uff1f<\/strong><br \/>\u5728\u9009\u62e9Python GUI\u5e93\u65f6\uff0c\u9700\u8981\u8003\u8651\u9879\u76ee\u7684\u9700\u6c42\u548c\u590d\u6742\u7a0b\u5ea6\u3002\u5e38\u89c1\u7684\u5e93\u5305\u62ecTkinter\u3001PyQt\u548cwxPython\u3002Tkinter\u662fPython\u81ea\u5e26\u7684\uff0c\u9002\u5408\u7b80\u5355\u5e94\u7528\uff1bPyQt\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u590d\u6742\u7684\u684c\u9762\u5e94\u7528\uff1bwxPython\u63d0\u4f9b\u539f\u751f\u5916\u89c2\uff0c\u9002\u5408\u9700\u8981\u4e0e\u7cfb\u7edf\u7d27\u5bc6\u96c6\u6210\u7684\u5e94\u7528\u3002\u4e86\u89e3\u6bcf\u4e2a\u5e93\u7684\u4f18\u7f3a\u70b9\uff0c\u7ed3\u5408\u9879\u76ee\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u5c06\u6709\u52a9\u4e8e\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u521b\u5efaGUI\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u5e03\u5c40\u7ba1\u7406\u5668\uff1f<\/strong><br \/>\u5728Python GUI\u5f00\u53d1\u4e2d\uff0c\u5e38\u7528\u7684\u5e03\u5c40\u7ba1\u7406\u5668\u6709Pack\u3001Grid\u548cPlace\u3002Pack\u9002\u7528\u4e8e\u7b80\u5355\u7684\u7ec4\u4ef6\u6392\u5217\uff0cGrid\u5141\u8bb8\u4ee5\u8868\u683c\u5f62\u5f0f\u7cbe\u786e\u63a7\u5236\u7ec4\u4ef6\u4f4d\u7f6e\uff0c\u800cPlace\u5219\u63d0\u4f9b\u7edd\u5bf9\u5b9a\u4f4d\u7684\u7075\u6d3b\u6027\u3002\u9009\u62e9\u5408\u9002\u7684\u5e03\u5c40\u7ba1\u7406\u5668\u53ef\u4ee5\u8ba9\u754c\u9762\u66f4\u52a0\u7f8e\u89c2\u5e76\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u8bd5Python GUI\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u8c03\u8bd5Python GUI\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528print\u8bed\u53e5\u3001logging\u6a21\u5757\u6216\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u4e2d\u7684\u8c03\u8bd5\u5de5\u5177\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u4e8b\u4ef6\u5904\u7406\u548c\u56de\u8c03\u51fd\u6570\uff0c\u5efa\u8bae\u5728\u5173\u952e\u4ee3\u7801\u4f4d\u7f6e\u6dfb\u52a0\u65e5\u5fd7\u8f93\u51fa\uff0c\u4ee5\u4fbf\u8ffd\u8e2a\u6267\u884c\u6d41\u7a0b\u3002\u6b64\u5916\uff0c\u4f7f\u7528IDE\u7684\u8c03\u8bd5\u529f\u80fd\uff0c\u53ef\u4ee5\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u76d1\u63a7\u53d8\u91cf\u72b6\u6001\uff0c\u5e2e\u52a9\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u6253\u5f00Python\u7684GUI\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5de5\u5177\u548c\u5e93\uff0c\u5305\u62ecTkinter\u3001PyQt\u3001wxPython\u7b49\u3002\u4f7f\u7528Tk [&hellip;]","protected":false},"author":3,"featured_media":985726,"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\/985720"}],"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=985720"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985720\/revisions"}],"predecessor-version":[{"id":985730,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985720\/revisions\/985730"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/985726"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=985720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=985720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=985720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}