{"id":982564,"date":"2024-12-27T07:11:52","date_gmt":"2024-12-26T23:11:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/982564.html"},"modified":"2024-12-27T07:11:54","modified_gmt":"2024-12-26T23:11:54","slug":"python%e5%a6%82%e4%bd%95%e5%86%99%e6%8c%89%e9%92%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/982564.html","title":{"rendered":"python\u5982\u4f55\u5199\u6309\u94ae"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211012\/ecb8f897-4974-461d-bd91-dfc129a3078b.webp\" alt=\"python\u5982\u4f55\u5199\u6309\u94ae\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u521b\u5efa\u6309\u94ae\uff0c\u901a\u5e38\u4f7f\u7528GUI\u5e93\u5982Tkinter\u3001PyQt\u6216Kivy\u3002\u6bcf\u4e2a\u5e93\u6709\u5176\u72ec\u7279\u7684\u8bed\u6cd5\u548c\u529f\u80fd\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u60a8\u7684\u5177\u4f53\u9700\u6c42\u548c\u504f\u597d\u3002<\/strong>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efa\u6309\u94ae\uff0c\u5e76\u8be6\u7ec6\u89e3\u91ca\u5176\u6b65\u9aa4\u548c\u76f8\u5173\u6982\u5ff5\u3002<\/p>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u63d0\u4f9b\u4e86\u521b\u5efa\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u8981\u5728Tkinter\u4e2d\u521b\u5efa\u6309\u94ae\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165Tkinter\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\uff0c\u63a5\u7740\u521b\u5efaButton\u5c0f\u90e8\u4ef6\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u7a97\u53e3\u4e2d\u3002\u6700\u540e\uff0c\u60a8\u9700\u8981\u4e3a\u6309\u94ae\u6307\u5b9a\u4e00\u4e2a\u547d\u4ee4\uff0c\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\u6267\u884c\u76f8\u5e94\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165Tkinter<\/h3>\n<\/p>\n<p><p>Tkinter\u901a\u5e38\u4e0ePython\u4e00\u8d77\u5b89\u88c5\uff0c\u56e0\u6b64\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002\u4f46\u662f\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u786e\u4fddTkinter\u5df2\u6b63\u786e\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u4e3b\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u5728Tkinter\u4e2d\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\uff0c\u8fd9\u662f\u6240\u6709\u5c0f\u90e8\u4ef6\u7684\u5bb9\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root = tk.Tk()<\/p>\n<p>root.title(&quot;\u6309\u94ae\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u6309\u94ae<\/h3>\n<\/p>\n<p><p>\u6309\u94ae\u662fTkinter\u4e2d\u5e38\u7528\u7684\u5c0f\u90e8\u4ef6\u4e4b\u4e00\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u4e0e\u7a0b\u5e8f\u4ea4\u4e92\u3002\u521b\u5efa\u6309\u94ae\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def on_button_click():<\/p>\n<p>    print(&quot;\u6309\u94ae\u88ab\u70b9\u51fb\u4e86\uff01&quot;)<\/p>\n<p>button = tk.Button(root, text=&quot;\u70b9\u51fb\u6211&quot;, command=on_button_click)<\/p>\n<p>button.pack(pady=20)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>Button<\/code>\u6784\u9020\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u6309\u94ae\u5c0f\u90e8\u4ef6\u3002<code>text<\/code>\u53c2\u6570\u6307\u5b9a\u6309\u94ae\u4e0a\u663e\u793a\u7684\u6587\u672c\uff0c\u800c<code>command<\/code>\u53c2\u6570\u6307\u5b9a\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\u8981\u6267\u884c\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8fd0\u884c\u4e3b\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u5b8c\u6240\u6709\u5c0f\u90e8\u4ef6\u540e\uff0c\u9700\u8981\u8fd0\u884c\u4e3b\u5faa\u73af\u4ee5\u663e\u793a\u7a97\u53e3\u5e76\u7b49\u5f85\u7528\u6237\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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><h3>\u4e94\u3001Tkinter\u6309\u94ae\u7684\u9ad8\u7ea7\u7279\u6027<\/h3>\n<\/p>\n<p><h4>1\u3001\u8bbe\u7f6e\u6309\u94ae\u7684\u5916\u89c2<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5404\u79cd\u9009\u9879\u8bbe\u7f6e\u6309\u94ae\u7684\u5916\u89c2\uff0c\u4f8b\u5982\u989c\u8272\u3001\u5b57\u4f53\u548c\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">button = tk.Button(root, text=&quot;\u70b9\u51fb\u6211&quot;, command=on_button_click,<\/p>\n<p>                   bg=&quot;blue&quot;, fg=&quot;white&quot;, font=(&quot;Helvetica&quot;, 16))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7981\u7528\u548c\u542f\u7528\u6309\u94ae<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u7981\u7528\u6216\u542f\u7528\u6309\u94ae\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">button.config(state=tk.DISABLED)  # \u7981\u7528\u6309\u94ae<\/p>\n<p>button.config(state=tk.NORMAL)    # \u542f\u7528\u6309\u94ae<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528\u56fe\u50cf\u4f5c\u4e3a\u6309\u94ae<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u56fe\u50cf\u800c\u4e0d\u662f\u6587\u672c\u6765\u521b\u5efa\u6309\u94ae\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">photo = tk.PhotoImage(file=&quot;image.png&quot;)<\/p>\n<p>button = tk.Button(root, image=photo, command=on_button_click)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5e94\u7528\u573a\u666f\u548c\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u54cd\u5e94\u7528\u6237\u4ea4\u4e92\uff1a<\/strong> \u6309\u94ae\u901a\u5e38\u7528\u4e8e\u89e6\u53d1\u7279\u5b9a\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u63d0\u4ea4\u8868\u5355\u3001\u542f\u52a8\u65b0\u7a97\u53e3\u6216\u6539\u53d8\u5e94\u7528\u7a0b\u5e8f\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b80\u6d01\u660e\u4e86\u7684\u8bbe\u8ba1\uff1a<\/strong> \u6309\u94ae\u7684\u8bbe\u8ba1\u5e94\u6613\u4e8e\u7406\u89e3\uff0c\u786e\u4fdd\u7528\u6237\u4e00\u773c\u5c31\u80fd\u660e\u767d\u6309\u94ae\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63d0\u4f9b\u53cd\u9988\uff1a<\/strong> \u5728\u6309\u94ae\u88ab\u70b9\u51fb\u540e\uff0c\u63d0\u4f9b\u53ca\u65f6\u7684\u53cd\u9988\u53ef\u4ee5\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u3002\u4f8b\u5982\uff0c\u6539\u53d8\u6309\u94ae\u7684\u989c\u8272\u6216\u663e\u793a\u4e00\u6761\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65e0\u969c\u788d\u8bbe\u8ba1\uff1a<\/strong> \u786e\u4fdd\u6309\u94ae\u5bf9\u4e8e\u6240\u6709\u7528\u6237\u90fd\u53ef\u7528\uff0c\u5305\u62ec\u90a3\u4e9b\u4f7f\u7528\u8f85\u52a9\u6280\u672f\u7684\u7528\u6237\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u5176\u4ed6GUI\u5e93\u7684\u6309\u94ae\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Tkinter\uff0c\u5176\u4ed6\u5e38\u7528\u7684GUI\u5e93\u8fd8\u6709PyQt\u548cKivy\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>PyQt<\/h4>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u9002\u7528\u4e8e\u9700\u8981\u590d\u6742\u7528\u6237\u754c\u9762\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QPushButton, QWidget<\/p>\n<p>app = QApplication([])<\/p>\n<p>window = QWidget()<\/p>\n<p>button = QPushButton(&#39;\u70b9\u51fb\u6211&#39;, window)<\/p>\n<p>button.clicked.connect(lambda: print(&quot;\u6309\u94ae\u88ab\u70b9\u51fb\u4e86\uff01&quot;))<\/p>\n<p>window.show()<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>Kivy<\/h4>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684GUI\u5e93\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5728\u591a\u4e2a\u5e73\u53f0\u4e0a\u8fd0\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.button import Button<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        return Button(text=&#39;\u70b9\u51fb\u6211&#39;, on_press=self.on_button_click)<\/p>\n<p>    def on_button_click(self, instance):<\/p>\n<p>        print(&quot;\u6309\u94ae\u88ab\u70b9\u51fb\u4e86\uff01&quot;)<\/p>\n<p>MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u521b\u5efa\u6309\u94ae\u4e3b\u8981\u901a\u8fc7GUI\u5e93\u5b9e\u73b0\uff0cTkinter\u662f\u6700\u5e38\u7528\u7684\u9009\u62e9\uff0c\u5176\u6b21\u662fPyQt\u548cKivy\u3002<\/strong> \u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u7279\u6027\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u9ad8\u6548\u5730\u5f00\u53d1\u5e94\u7528\u7a0b\u5e8f\u3002\u901a\u8fc7\u5408\u7406\u8bbe\u8ba1\u6309\u94ae\u7684\u5916\u89c2\u548c\u529f\u80fd\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u6613\u7528\u6027\u548c\u53ef\u8bbf\u95ee\u6027\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\u6309\u94ae\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\uff0c\u5176\u4e2d\u5305\u62ec\u6309\u94ae\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5Tkinter\uff0c\u901a\u5e38\u5b83\u4f1a\u968fPython\u5b89\u88c5\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6309\u94ae\uff1a<\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\ndef on_button_click():\n    print(&quot;\u6309\u94ae\u88ab\u70b9\u51fb\u4e86\uff01&quot;)\n\nroot = tk.Tk()\nbutton = tk.Button(root, text=&quot;\u70b9\u51fb\u6211&quot;, command=on_button_click)\nbutton.pack()\n\nroot.mainloop()\n<\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u540e\uff0c\u4f1a\u51fa\u73b0\u4e00\u4e2a\u7a97\u53e3\uff0c\u70b9\u51fb\u6309\u94ae\u65f6\u4f1a\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u4fe1\u606f\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u521b\u5efaPython\u6309\u94ae\uff1f<\/strong><br \/>\u9664\u4e86Tkinter\uff0cPython\u8fd8\u652f\u6301\u5176\u4ed6\u591a\u4e2a\u5e93\u6765\u521b\u5efa\u6309\u94ae\u548cGUI\u5e94\u7528\u3002\u4f8b\u5982\uff0cPyQt\u548cKivy\u662f\u975e\u5e38\u6d41\u884c\u7684\u9009\u62e9\u3002PyQt\u63d0\u4f9b\u4e86\u4e00\u5957\u5b8c\u6574\u7684\u529f\u80fd\u4e30\u5bcc\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5f00\u53d1\u590d\u6742\u7684\u684c\u9762\u5e94\u7528\uff0c\u800cKivy\u5219\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u6846\u67b6\uff0c\u9002\u5408\u4e8e\u591a\u70b9\u89e6\u63a7\u548c\u79fb\u52a8\u8bbe\u5907\u5e94\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u6309\u94ae\u7684\u6837\u5f0f\u548c\u529f\u80fd\uff1f<\/strong><br \/>\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6309\u94ae\u7684\u5c5e\u6027\u6765\u81ea\u5b9a\u4e49\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>bg<\/code>\u548c<code>fg<\/code>\u5c5e\u6027\u6765\u6539\u53d8\u80cc\u666f\u548c\u524d\u666f\u8272\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7<code>font<\/code>\u5c5e\u6027\u8bbe\u7f6e\u5b57\u4f53\u6837\u5f0f\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e0d\u540c\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u6765\u5b9e\u73b0\u591a\u79cd\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4fee\u6539<code>command<\/code>\u53c2\u6570\u6765\u7ed1\u5b9a\u4e0d\u540c\u7684\u51fd\u6570\u5230\u6309\u94ae\u4e0a\u3002<\/p>\n<pre><code class=\"language-python\">button = tk.Button(root, text=&quot;\u81ea\u5b9a\u4e49\u6309\u94ae&quot;, bg=&quot;blue&quot;, fg=&quot;white&quot;, font=(&quot;Arial&quot;, 12), command=some_function)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u7b80\u5355\u7684\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u51fa\u65e2\u7f8e\u89c2\u53c8\u529f\u80fd\u4e30\u5bcc\u7684\u6309\u94ae\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u521b\u5efa\u6309\u94ae\uff0c\u901a\u5e38\u4f7f\u7528GUI\u5e93\u5982Tkinter\u3001PyQt\u6216Kivy\u3002\u6bcf\u4e2a\u5e93\u6709\u5176\u72ec\u7279\u7684\u8bed\u6cd5\u548c\u529f\u80fd\uff0c\u9009 [&hellip;]","protected":false},"author":3,"featured_media":982575,"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\/982564"}],"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=982564"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982564\/revisions"}],"predecessor-version":[{"id":982579,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982564\/revisions\/982579"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/982575"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=982564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=982564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=982564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}