{"id":1020743,"date":"2024-12-27T13:15:06","date_gmt":"2024-12-27T05:15:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1020743.html"},"modified":"2024-12-27T13:15:08","modified_gmt":"2024-12-27T05:15:08","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8button%e4%ba%8b%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1020743.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528button\u4e8b\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25163118\/2bf40b84-5ba2-4c8b-b806-9ae722d6ac8b.webp\" alt=\"python\u5982\u4f55\u8c03\u7528button\u4e8b\u4ef6\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u8c03\u7528\u6309\u94ae\u4e8b\u4ef6\u901a\u5e38\u9700\u8981\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93<\/strong>\uff0c\u5982Tkinter\u3001PyQt\u6216Kivy\u7b49\u3002<strong>\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u521b\u5efa\u6309\u94ae\u5e76\u5b9a\u4e49\u5176\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u7684\u529f\u80fd<\/strong>\u3002\u901a\u8fc7\u4e3a\u6309\u94ae\u7ed1\u5b9a\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u81ea\u52a8\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u3002\u8fd9\u91cc\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tkinter\u5e93\u5b9e\u73b0\u6309\u94ae\u4e8b\u4ef6\u7684\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Tkinter\u5e93\u7684\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c<strong>\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u800c\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762<\/strong>\u3002Tkinter\u662f\u8de8\u5e73\u53f0\u7684\uff0c\u8fd9\u610f\u5473\u7740\u60a8\u53ef\u4ee5\u5728Windows\u3001macOS\u548cLinux\u4e0a\u4f7f\u7528\u76f8\u540c\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>Tkinter\u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7a97\u53e3\uff08Window\uff09<\/strong>\uff1aTkinter\u7684\u57fa\u672c\u5355\u4f4d\uff0c\u7528\u4e8e\u5bb9\u7eb3\u5176\u4ed6\u7ec4\u4ef6\u3002<\/li>\n<li><strong>\u7ec4\u4ef6\uff08Widget\uff09<\/strong>\uff1a\u6784\u6210\u754c\u9762\u7684\u5143\u7d20\uff0c\u5982\u6309\u94ae\u3001\u6807\u7b7e\u3001\u6587\u672c\u6846\u7b49\u3002<\/li>\n<li><strong>\u4e8b\u4ef6\uff08Event\uff09<\/strong>\uff1a\u7528\u6237\u4e0e\u754c\u9762\u4ea4\u4e92\u7684\u884c\u4e3a\uff0c\u5982\u70b9\u51fb\u3001\u8f93\u5165\u7b49\u3002<\/li>\n<\/ul>\n<p><h4>\u5b89\u88c5\u4e0e\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>Tkinter\u901a\u5e38\u968fPython\u5b89\u88c5\u5305\u4e00\u8d77\u5b89\u88c5\uff0c\u65e0\u9700\u5355\u72ec\u5b89\u88c5\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\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><h3>\u4e8c\u3001\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Tkinter\u7a97\u53e3<\/h3>\n<\/p>\n<p><h4>\u521b\u5efa\u4e3b\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2aTkinter\u4e3b\u7a97\u53e3\u662f\u5b9e\u73b0\u4efb\u4f55GUI\u5e94\u7528\u7a0b\u5e8f\u7684\u7b2c\u4e00\u6b65\u3002\u53ef\u4ee5\u901a\u8fc7<code>Tk()<\/code>\u7c7b\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root = tk.Tk()<\/p>\n<p>root.title(&quot;Button Event Example&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>Tk()<\/code>\uff1a\u7528\u4e8e\u521b\u5efa\u4e00\u4e2aTkinter\u4e3b\u7a97\u53e3\u3002<\/li>\n<li><code>title()<\/code>\uff1a\u7528\u4e8e\u8bbe\u7f6e\u7a97\u53e3\u6807\u9898\u3002<\/li>\n<li><code>geometry()<\/code>\uff1a\u7528\u4e8e\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<p><h4>\u6dfb\u52a0\u4e00\u4e2a\u6309\u94ae<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6dfb\u52a0\u4e00\u4e2a\u6309\u94ae\u662f\u5b9e\u73b0\u6309\u94ae\u4e8b\u4ef6\u7684\u5173\u952e\u3002\u53ef\u4ee5\u4f7f\u7528<code>Button<\/code>\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def on_button_click():<\/p>\n<p>    print(&quot;Button was clicked!&quot;)<\/p>\n<p>button = tk.Button(root, text=&quot;Click Me&quot;, command=on_button_click)<\/p>\n<p>button.pack(pady=20)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>Button()<\/code>\uff1a\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u6309\u94ae\u3002\n<ul>\n<li><code>text<\/code>\uff1a\u6309\u94ae\u4e0a\u663e\u793a\u7684\u6587\u672c\u3002<\/li>\n<li><code>command<\/code>\uff1a\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\u8c03\u7528\u7684\u51fd\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<li><code>pack()<\/code>\uff1a\u7528\u4e8e\u7ba1\u7406\u7ec4\u4ef6\u7684\u5e03\u5c40\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u7684\u5b9a\u4e49<\/h3>\n<\/p>\n<p><h4>\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u7684\u4f5c\u7528<\/h4>\n<\/p>\n<p><p>\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u662f\u5728\u4e8b\u4ef6\u89e6\u53d1\u65f6\u88ab\u8c03\u7528\u7684\u51fd\u6570\u3002\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>on_button_click<\/code>\u51fd\u6570\u662f\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\u8c03\u7528\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def on_button_click():<\/p>\n<p>    print(&quot;Button was clicked!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u60a8\u60f3\u8981\u5728\u6309\u94ae\u70b9\u51fb\u65f6\u6267\u884c\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h4>\u6dfb\u52a0\u66f4\u591a\u529f\u80fd<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\u5b9e\u73b0\u66f4\u591a\u590d\u6742\u7684\u903b\u8f91\uff0c\u4f8b\u5982\u4fee\u6539\u754c\u9762\u3001\u6267\u884c\u8ba1\u7b97\u6216\u4e0e\u5176\u4ed6\u7cfb\u7edf\u7ec4\u4ef6\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def on_button_click():<\/p>\n<p>    label.config(text=&quot;Button was clicked!&quot;)<\/p>\n<p>    # \u5176\u4ed6\u64cd\u4f5c\uff0c\u5982\u66f4\u65b0\u6570\u636e\u5e93\u3001\u53d1\u9001\u7f51\u7edc\u8bf7\u6c42\u7b49<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5176\u4ed6\u5e93\u5b9e\u73b0\u6309\u94ae\u4e8b\u4ef6<\/h3>\n<\/p>\n<p><p>\u867d\u7136Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u4f46\u5176\u4ed6\u5e93\u5982PyQt\u548cKivy\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u4e0b\u9762\u7b80\u8981\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u3002<\/p>\n<\/p>\n<p><h4>PyQt<\/h4>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684GUI\u5e93\uff0c\u57fa\u4e8eQt\u6846\u67b6\uff0c\u9002\u5408\u9700\u8981\u66f4\u590d\u6742\u754c\u9762\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QPushButton, QLabel, QVBoxLayout, QWidget<\/p>\n<p>def on_button_click():<\/p>\n<p>    label.setText(&quot;Button was clicked!&quot;)<\/p>\n<p>app = QApplication([])<\/p>\n<p>window = QWidget()<\/p>\n<p>layout = QVBoxLayout()<\/p>\n<p>label = QLabel(&quot;Hello, PyQt!&quot;)<\/p>\n<p>button = QPushButton(&quot;Click Me&quot;)<\/p>\n<p>button.clicked.connect(on_button_click)<\/p>\n<p>layout.addWidget(label)<\/p>\n<p>layout.addWidget(button)<\/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>Kivy<\/h4>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u9002\u5408\u5f00\u53d1\u8de8\u5e73\u53f0\u5e94\u7528\u7684\u5e93\uff0c\u652f\u6301\u591a\u70b9\u89e6\u63a7\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>from kivy.uix.label import Label<\/p>\n<p>from kivy.uix.boxlayout import BoxLayout<\/p>\n<p>class TestApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        layout = BoxLayout(orientation=&#39;vertical&#39;)<\/p>\n<p>        self.label = Label(text=&#39;Hello, Kivy!&#39;)<\/p>\n<p>        button = Button(text=&#39;Click Me&#39;)<\/p>\n<p>        button.bind(on_press=self.on_button_click)<\/p>\n<p>        layout.add_widget(self.label)<\/p>\n<p>        layout.add_widget(button)<\/p>\n<p>        return layout<\/p>\n<p>    def on_button_click(self, instance):<\/p>\n<p>        self.label.text = &quot;Button was clicked!&quot;<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    TestApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6df1\u5165\u7406\u89e3\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b<\/h3>\n<\/p>\n<p><h4>\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u7684\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c<strong>\u7a0b\u5e8f\u7684\u6267\u884c\u662f\u7531\u4e8b\u4ef6\u89e6\u53d1\u7684<\/strong>\u3002\u5728GUI\u5e94\u7528\u4e2d\uff0c\u4e8b\u4ef6\u5305\u62ec\u7528\u6237\u7684\u8f93\u5165\u64cd\u4f5c\uff0c\u5982\u70b9\u51fb\u3001\u952e\u5165\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4e8b\u4ef6\u5faa\u73af<\/strong>\uff1a\u7b49\u5f85\u5e76\u5206\u53d1\u4e8b\u4ef6\u7684\u5faa\u73af\u3002<\/li>\n<li><strong>\u4e8b\u4ef6\u5904\u7406\u5668<\/strong>\uff1a\u5904\u7406\u7279\u5b9a\u4e8b\u4ef6\u7684\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p><h4>\u4e8b\u4ef6\u5faa\u73af\u5728Tkinter\u4e2d\u7684\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u5728Tkinter\u4e2d\uff0c\u4e8b\u4ef6\u5faa\u73af\u7531<code>mainloop()<\/code>\u65b9\u6cd5\u5b9e\u73b0\u3002\u5f53\u8c03\u7528<code>mainloop()<\/code>\u65f6\uff0c\u7a0b\u5e8f\u5f00\u59cb\u7b49\u5f85\u7528\u6237\u8f93\u5165\u5e76\u54cd\u5e94\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>mainloop()<\/code>\uff1a\u542f\u52a8Tkinter\u7684\u4e8b\u4ef6\u5faa\u73af\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u8fdb\u9636\u529f\u80fd\u4e0e\u4f18\u5316<\/h3>\n<\/p>\n<p><h4>\u6dfb\u52a0\u66f4\u591a\u7ec4\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u7ec4\u4ef6\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u7528\u6237\u754c\u9762\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">label = tk.Label(root, text=&quot;Welcome to Tkinter!&quot;)<\/p>\n<p>entry = tk.Entry(root)<\/p>\n<p>label.pack()<\/p>\n<p>entry.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>Label()<\/code>\uff1a\u7528\u4e8e\u663e\u793a\u6587\u672c\u3002<\/li>\n<li><code>Entry()<\/code>\uff1a\u7528\u4e8e\u63a5\u53d7\u7528\u6237\u8f93\u5165\u3002<\/li>\n<\/ul>\n<p><h4>\u4f7f\u7528Frame\u5e03\u5c40<\/h4>\n<\/p>\n<p><p><code>Frame<\/code>\u662fTkinter\u4e2d\u7528\u4e8e\u7ec4\u7ec7\u7ec4\u4ef6\u7684\u5bb9\u5668\uff0c\u9002\u5408\u590d\u6742\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">frame = tk.Frame(root)<\/p>\n<p>frame.pack()<\/p>\n<p>label = tk.Label(frame, text=&quot;Inside Frame&quot;)<\/p>\n<p>label.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4e8b\u4ef6\u7ed1\u5b9a<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>command<\/code>\u5c5e\u6027\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>bind<\/code>\u65b9\u6cd5\u4e3a\u7ec4\u4ef6\u7ed1\u5b9a\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">button.bind(&quot;&lt;Button-1&gt;&quot;, lambda e: print(&quot;Button was clicked with bind!&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u8c03\u8bd5\u4e0e\u4f18\u5316<\/h3>\n<\/p>\n<p><h4>\u8c03\u8bd5\u6280\u5de7<\/h4>\n<\/p>\n<ul>\n<li><strong>\u68c0\u67e5\u5f02\u5e38<\/strong>\uff1a\u786e\u4fdd\u5904\u7406\u6240\u6709\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\u3002<\/li>\n<li><strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong>\uff1a\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u6765\u8ddf\u8e2a\u5e94\u7528\u7a0b\u5e8f\u7684\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<p><h4>\u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<ul>\n<li><strong>\u51cf\u5c11\u91cd\u7ed8<\/strong>\uff1a\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u754c\u9762\u91cd\u7ed8\uff0c\u4ee5\u63d0\u5347\u6027\u80fd\u3002<\/li>\n<li><strong>\u5f02\u6b65\u5904\u7406<\/strong>\uff1a\u5bf9\u4e8e\u8017\u65f6\u64cd\u4f5c\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65IO\uff0c\u4ee5\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u6df1\u5165\u4e86\u89e3\u4e86\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528\u6309\u94ae\u4e8b\u4ef6\u3002\u4f7f\u7528Tkinter\u5e93\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684GUI\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u4f7f\u7528\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u54cd\u5e94\u7528\u6237\u7684\u64cd\u4f5c\u3002\u6211\u4eec\u8fd8\u63a2\u8ba8\u4e86\u4f7f\u7528\u5176\u4ed6\u5e93\uff08\u5982PyQt\u548cKivy\uff09\u5b9e\u73b0\u7c7b\u4f3c\u529f\u80fd\u7684\u65b9\u6cd5\u3002\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u662fGUI\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u6838\u5fc3\uff0c\u901a\u8fc7\u5408\u7406\u8bbe\u8ba1\u4e8b\u4ef6\u5904\u7406\u673a\u5236\uff0c\u53ef\u4ee5\u521b\u5efa\u7528\u6237\u4f53\u9a8c\u826f\u597d\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u6700\u540e\uff0c\u901a\u8fc7\u6dfb\u52a0\u66f4\u591a\u7ec4\u4ef6\u3001\u4f18\u5316\u5e03\u5c40\u548c\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u5e94\u7528\u7684\u529f\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u6309\u94ae\u5e76\u7ed1\u5b9a\u4e8b\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\uff0c\u5e76\u901a\u8fc7\u6309\u94ae\u7ed1\u5b9a\u4e8b\u4ef6\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165Tkinter\u6a21\u5757\uff0c\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\uff0c\u7136\u540e\u6dfb\u52a0\u6309\u94ae\u5e76\u4f7f\u7528<code>command<\/code>\u53c2\u6570\u5c06\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e0e\u6309\u94ae\u5173\u8054\u3002\u793a\u4f8b\u5982\u4e0b\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!&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>\u4ee5\u4e0a\u4ee3\u7801\u4f1a\u5728\u7a97\u53e3\u4e2d\u521b\u5efa\u4e00\u4e2a\u6309\u94ae\uff0c\u70b9\u51fb\u6309\u94ae\u65f6\u4f1a\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u4fe1\u606f\u3002<\/p>\n<p><strong>\u6309\u94ae\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u53ef\u4ee5\u63a5\u6536\u53c2\u6570\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>lambda<\/code>\u6216<code>functools.partial<\/code>\u6765\u4f20\u9012\u53c2\u6570\u7ed9\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>lambda<\/code>\u53ef\u4ee5\u5982\u6b64\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">def on_button_click(param):\n    print(f&quot;\u6309\u94ae\u88ab\u70b9\u51fb\uff0c\u53c2\u6570\u4e3a: {param}&quot;)\n\nbutton = tk.Button(root, text=&quot;\u70b9\u51fb\u6211&quot;, command=lambda: on_button_click(&quot;Hello&quot;))\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6309\u94ae\u70b9\u51fb\u65f6\u4f1a\u4f20\u9012\u5b57\u7b26\u4e32&quot;Hello&quot;\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6309\u94ae\u4e8b\u4ef6\u4e2d\u66f4\u65b0GUI\u5143\u7d20\uff1f<\/strong><br \/>\u5728\u6309\u94ae\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u5176\u4ed6GUI\u5143\u7d20\u7684\u5c5e\u6027\uff0c\u4f8b\u5982\u6807\u7b7e\u6216\u6587\u672c\u6846\u7684\u5185\u5bb9\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u8fd9\u4e9b\u5143\u7d20\u7684<code>config<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-python\">label = tk.Label(root, text=&quot;\u521d\u59cb\u6587\u672c&quot;)\nlabel.pack()\n\ndef update_label():\n    label.config(text=&quot;\u6587\u672c\u5df2\u66f4\u65b0!&quot;)\n\nbutton = tk.Button(root, text=&quot;\u66f4\u65b0\u6587\u672c&quot;, command=update_label)\nbutton.pack()\n<\/code><\/pre>\n<p>\u5f53\u7528\u6237\u70b9\u51fb\u201c\u66f4\u65b0\u6587\u672c\u201d\u6309\u94ae\u65f6\uff0c\u6807\u7b7e\u7684\u6587\u672c\u5c06\u88ab\u66f4\u6539\u4e3a\u201c\u6587\u672c\u5df2\u66f4\u65b0!\u201d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8c03\u7528\u6309\u94ae\u4e8b\u4ef6\u901a\u5e38\u9700\u8981\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\uff0c\u5982Tkinter\u3001PyQt\u6216Kivy\u7b49\u3002\u8fd9 [&hellip;]","protected":false},"author":3,"featured_media":1020748,"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\/1020743"}],"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=1020743"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1020743\/revisions"}],"predecessor-version":[{"id":1020750,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1020743\/revisions\/1020750"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1020748"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1020743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1020743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1020743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}