{"id":1080700,"date":"2025-01-08T12:32:38","date_gmt":"2025-01-08T04:32:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080700.html"},"modified":"2025-01-08T12:32:40","modified_gmt":"2025-01-08T04:32:40","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80%e4%b8%80%e4%b8%aa%e7%aa%97%e5%8f%a3-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1080700.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00\u4e00\u4e2a\u7a97\u53e3"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183116\/aff9af21-689a-4a29-9e6d-afc94292cdc3.webp\" alt=\"python\u5982\u4f55\u6253\u5f00\u4e00\u4e2a\u7a97\u53e3\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u6253\u5f00\u4e00\u4e2a\u7a97\u53e3<\/strong>\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528Tkinter\u5e93\u3001\u4f7f\u7528PyQt\u5e93\u3001\u4f7f\u7528wxPython\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c<strong>Tkinter\u5e93\u662fPython\u6807\u51c6\u5e93\u4e2d\u81ea\u5e26\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e93<\/strong>\uff0c\u6240\u4ee5\u6700\u5e38\u7528\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Tkinter\u5e93\u6253\u5f00\u4e00\u4e2a\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Tkinter\u5e93<\/h3>\n<\/p>\n<p><h4>\u4ec0\u4e48\u662fTkinter<\/h4>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\u3002\u5b83\u662fPython\u5185\u7f6e\u7684\u56fe\u5f62\u754c\u9762\u5e93\uff0c\u4f7f\u7528Tkinter\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u521b\u5efa\u56fe\u5f62\u754c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002Tkinter\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u63a5\u53e3\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u56fe\u5f62\u754c\u9762\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Tkinter<\/h4>\n<\/p>\n<p><p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0cTkinter\u5e93\u662f\u968fPython\u5b89\u88c5\u5305\u4e00\u540c\u5b89\u88c5\u7684\u3002\u5982\u679c\u4f60\u7684Python\u73af\u5883\u4e2d\u6ca1\u6709\u5b89\u88c5Tkinter\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Tkinter\u521b\u5efa\u7b80\u5355\u7a97\u53e3\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u6807\u9898<\/strong><\/h2>\n<p>root.title(&quot;\u6211\u7684\u7b2c\u4e00\u4e2aTkinter\u7a97\u53e3&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.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\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>tkinter<\/code>\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u4e3b\u7a97\u53e3\u5bf9\u8c61<code>root<\/code>\u3002\u63a5\u4e0b\u6765\uff0c\u8bbe\u7f6e\u7a97\u53e3\u7684\u6807\u9898\u548c\u5927\u5c0f\uff0c\u6700\u540e\u8c03\u7528<code>root.mainloop()<\/code>\u8fdb\u5165\u4e3b\u5faa\u73af\uff0c\u4f7f\u7a97\u53e3\u4fdd\u6301\u663e\u793a\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PyQt\u5e93<\/h3>\n<\/p>\n<p><h4>\u4ec0\u4e48\u662fPyQt<\/h4>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u7528\u4e8ePython\u7684\u8de8\u5e73\u53f0\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e93\u3002\u5b83\u662fQt\u5e93\u7684Python\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u5f3a\u5927\u7684\u529f\u80fd\u3002PyQt\u4e0eTkinter\u76f8\u6bd4\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u63a7\u4ef6\u548c\u66f4\u5f3a\u7684\u529f\u80fd\uff0c\u4f46\u76f8\u5e94\u5730\u5b66\u4e60\u66f2\u7ebf\u4e5f\u66f4\u9661\u5ced\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5PyQt<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5PyQt\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyqt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528PyQt5\u521b\u5efa\u7b80\u5355\u7a97\u53e3\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QWidget<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61<\/strong><\/h2>\n<p>app = QApplication(sys.argv)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>window = QWidget()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u6807\u9898<\/strong><\/h2>\n<p>window.setWindowTitle(&quot;\u6211\u7684\u7b2c\u4e00\u4e2aPyQt5\u7a97\u53e3&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>window.resize(400, 300)<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>window.show()<\/p>\n<h2><strong>\u8fdb\u5165\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>sys<\/code>\u548c<code>PyQt5.QtWidgets<\/code>\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61<code>app<\/code>\u3002\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u4e3b\u7a97\u53e3\u5bf9\u8c61<code>window<\/code>\uff0c\u8bbe\u7f6e\u7a97\u53e3\u7684\u6807\u9898\u548c\u5927\u5c0f\uff0c\u6700\u540e\u8c03\u7528<code>window.show()<\/code>\u663e\u793a\u7a97\u53e3\uff0c\u5e76\u8c03\u7528<code>sys.exit(app.exec_())<\/code>\u8fdb\u5165\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528wxPython\u5e93<\/h3>\n<\/p>\n<p><h4>\u4ec0\u4e48\u662fwxPython<\/h4>\n<\/p>\n<p><p>wxPython\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Python\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e93\u3002\u5b83\u662fwxWidgets\u5e93\u7684Python\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u5f3a\u5927\u7684\u529f\u80fd\u3002wxPython\u4e0ePyQt\u7c7b\u4f3c\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u63a7\u4ef6\u548c\u66f4\u5f3a\u7684\u529f\u80fd\uff0c\u4f46\u76f8\u5e94\u5730\u5b66\u4e60\u66f2\u7ebf\u4e5f\u66f4\u9661\u5ced\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5wxPython<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5wxPython\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install wxPython<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528wxPython\u521b\u5efa\u7b80\u5355\u7a97\u53e3\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wx<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61<\/strong><\/h2>\n<p>app = wx.App()<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>frame = wx.Frame(None, title=&quot;\u6211\u7684\u7b2c\u4e00\u4e2awxPython\u7a97\u53e3&quot;, size=(400, 300))<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>frame.Show()<\/p>\n<h2><strong>\u8fdb\u5165\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>app.MainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>wx<\/code>\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61<code>app<\/code>\u3002\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u4e3b\u7a97\u53e3\u5bf9\u8c61<code>frame<\/code>\uff0c\u8bbe\u7f6e\u7a97\u53e3\u7684\u6807\u9898\u548c\u5927\u5c0f\uff0c\u6700\u540e\u8c03\u7528<code>frame.Show()<\/code>\u663e\u793a\u7a97\u53e3\uff0c\u5e76\u8c03\u7528<code>app.MainLoop()<\/code>\u8fdb\u5165\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ecb\u7ecd\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528Python\u6253\u5f00\u4e00\u4e2a\u7a97\u53e3\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u6709Tkinter\u3001PyQt\u548cwxPython\u3002<strong>\u5bf9\u4e8e\u521d\u5b66\u8005\uff0c\u63a8\u8350\u4f7f\u7528Tkinter\u5e93<\/strong>\uff0c\u56e0\u4e3a\u5b83\u662fPython\u7684\u6807\u51c6\u5e93\uff0c\u5b89\u88c5\u548c\u4f7f\u7528\u90fd\u975e\u5e38\u65b9\u4fbf\u3002<strong>\u5bf9\u4e8e\u6709\u4e00\u5b9a\u7f16\u7a0b\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528PyQt\u6216wxPython\u5e93<\/strong>\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u63a7\u4ef6\u548c\u66f4\u5f3a\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u56fe\u5f62\u754c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u7a97\u53e3\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002\u6700\u5e38\u89c1\u7684\u5e93\u662fTkinter\u3002\u8981\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7a97\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\nroot = tk.Tk()\nroot.title(&quot;\u6211\u7684\u7a97\u53e3&quot;)\nroot.geometry(&quot;400x300&quot;)\nroot.mainloop()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u521b\u5efa\u4e00\u4e2a\u6807\u9898\u4e3a\u201c\u6211\u7684\u7a97\u53e3\u201d\u7684400&#215;300\u50cf\u7d20\u7684\u7a97\u53e3\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528\u5176\u4ed6\u5e93\u6253\u5f00\u7a97\u53e3\u6709\u4ec0\u4e48\u9009\u62e9\uff1f<\/strong><br \/>\u9664\u4e86Tkinter\uff0cPython\u8fd8\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u7a97\u53e3\uff0c\u4f8b\u5982PyQt\u3001wxPython\u548cKivy\u3002PyQt\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u7ec4\u4ef6\u548c\u6837\u5f0f\uff0c\u9002\u5408\u6784\u5efa\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u3002wxPython\u5219\u66f4\u8d34\u8fd1\u539f\u751f\u754c\u9762\uff0c\u9002\u5408\u9700\u8981\u8de8\u5e73\u53f0\u7684\u9879\u76ee\u3002Kivy\u7279\u522b\u9002\u5408\u4e8e\u89e6\u63a7\u754c\u9762\u548c\u79fb\u52a8\u8bbe\u5907\u5e94\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u7a97\u53e3\u7684\u5916\u89c2\u548c\u884c\u4e3a\uff1f<\/strong><br \/>\u4f7f\u7528Tkinter\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7a97\u53e3\u7684\u6807\u9898\u3001\u5927\u5c0f\u3001\u80cc\u666f\u989c\u8272\u7b49\u6765\u5b9a\u5236\u7a97\u53e3\u7684\u5916\u89c2\u3002\u53ef\u4ee5\u4f7f\u7528<code>root.config(bg=&#39;color&#39;)<\/code>\u65b9\u6cd5\u66f4\u6539\u80cc\u666f\u989c\u8272\uff0c\u6216\u4f7f\u7528<code>root.geometry(&quot;\u5bbd\u5ea6x\u9ad8\u5ea6&quot;)<\/code>\u6765\u8c03\u6574\u7a97\u53e3\u5927\u5c0f\u3002\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u6309\u94ae\u3001\u6807\u7b7e\u7b49\u7ec4\u4ef6\uff0c\u589e\u5f3a\u7528\u6237\u4ea4\u4e92\u4f53\u9a8c\u3002\u901a\u8fc7\u7ed1\u5b9a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u60a8\u8fd8\u53ef\u4ee5\u63a7\u5236\u7a97\u53e3\u7684\u884c\u4e3a\uff0c\u4f8b\u5982\u54cd\u5e94\u7528\u6237\u7684\u70b9\u51fb\u6216\u952e\u76d8\u8f93\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u6253\u5f00\u4e00\u4e2a\u7a97\u53e3\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528Tkinter\u5e93\u3001\u4f7f\u7528PyQt\u5e93\u3001\u4f7f\u7528wxPython\u5e93\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1080704,"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\/1080700"}],"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=1080700"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080700\/revisions"}],"predecessor-version":[{"id":1080707,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080700\/revisions\/1080707"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1080704"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1080700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1080700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1080700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}