{"id":1179972,"date":"2025-01-15T18:31:09","date_gmt":"2025-01-15T10:31:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179972.html"},"modified":"2025-01-15T18:31:12","modified_gmt":"2025-01-15T10:31:12","slug":"%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96python%e6%8c%89%e9%92%ae%e5%8d%95%e5%87%bb%e4%ba%8b%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179972.html","title":{"rendered":"\u5982\u4f55\u83b7\u53d6python\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25114232\/9d60dfe6-4bf1-4687-a022-2a4e6e382375.webp\" alt=\"\u5982\u4f55\u83b7\u53d6python\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\" \/><\/p>\n<p><p> <strong>\u83b7\u53d6Python\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Tkinter\u3001PyQt\u3001Kivy\u7b49\u6846\u67b6\u3002<\/strong>\u5728\u8fd9\u4e9b\u6846\u67b6\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662fTkinter\uff0c\u56e0\u4e3a\u5b83\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u6613\u4e8e\u4f7f\u7528\u548c\u5b66\u4e60\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tkinter\u6765\u5b9e\u73b0\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Tkinter\u83b7\u53d6\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\uff1a<\/strong><\/p>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u4e8b\u4ef6\u5904\u7406\u673a\u5236\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u548c\u5feb\u901f\u5f00\u53d1\u3002\u8981\u4f7f\u7528Tkinter\u5b9e\u73b0\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\uff0c\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u521b\u5efa\u7a97\u53e3\u3001\u521b\u5efa\u6309\u94ae\u3001\u7ed1\u5b9a\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165Tkinter\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165Tkinter\u5e93\u3002Tkinter\u5728Python\u7684\u6807\u51c6\u5e93\u4e2d\uff0c\u4e0d\u9700\u8981\u989d\u5916\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>\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\uff0c\u8fd9\u662f\u6240\u6709\u63a7\u4ef6\u7684\u5bb9\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root = tk.Tk()<\/p>\n<p>root.title(&quot;Button Click Example&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\u63a7\u4ef6<\/h3>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u6309\u94ae\u63a7\u4ef6\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u4e3b\u7a97\u53e3\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">button = tk.Button(root, text=&quot;Click Me&quot;)<\/p>\n<p>button.pack(pady=20)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b9a\u4e49\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5728\u6309\u94ae\u88ab\u5355\u51fb\u65f6\u6267\u884c\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<p><h3>\u4e94\u3001\u7ed1\u5b9a\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6<\/h3>\n<\/p>\n<p><p>\u5c06\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u7ed1\u5b9a\u5230\u5b9a\u4e49\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">button.config(command=on_button_click)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u542f\u52a8\u4e3b\u4e8b\u4ef6\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u542f\u52a8\u4e3b\u4e8b\u4ef6\u5faa\u73af\uff0c\u4ee5\u4fbf\u7a0b\u5e8f\u80fd\u591f\u54cd\u5e94\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><p>\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_button_click():<\/p>\n<p>    print(&quot;Button was clicked!&quot;)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Button Click Example&quot;)<\/p>\n<p>root.geometry(&quot;300x200&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>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>PyQt\u5b9e\u73b0\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\uff1a<\/strong><\/h3>\n<\/p>\n<p><p>PyQt\u662f\u53e6\u4e00\u79cd\u6d41\u884c\u7684Python GUI\u5e93\uff0c\u5b83\u57fa\u4e8eQt\u6846\u67b6\uff0c\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u5f00\u53d1\u590d\u6742\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002\u4e0b\u9762\u662f\u4f7f\u7528PyQt\u5b9e\u73b0\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5PyQt\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5PyQt\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyQt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bfc\u5165PyQt\u5e93<\/h3>\n<\/p>\n<p><p>\u5bfc\u5165PyQt\u5e93\u4e2d\u7684\u5fc5\u8981\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u4e3b\u7a97\u53e3\u7c7b<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7ee7\u627f\u81eaQWidget\u7684\u4e3b\u7a97\u53e3\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MainWindow(QWidget):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.initUI()<\/p>\n<p>    def initUI(self):<\/p>\n<p>        self.setWindowTitle(&quot;Button Click Example&quot;)<\/p>\n<p>        self.setGeometry(100, 100, 300, 200)<\/p>\n<p>        layout = QVBoxLayout()<\/p>\n<p>        self.button = QPushButton(&quot;Click Me&quot;, self)<\/p>\n<p>        self.button.clicked.connect(self.on_button_click)<\/p>\n<p>        layout.addWidget(self.button)<\/p>\n<p>        self.setLayout(layout)<\/p>\n<p>    def on_button_click(self):<\/p>\n<p>        print(&quot;Button was clicked!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b\u5e76\u663e\u793a\u4e3b\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u521b\u5efaQApplication\u5b9e\u4f8b\u5e76\u663e\u793a\u4e3b\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if __name__ == &#39;__main__&#39;:<\/p>\n<p>    import sys<\/p>\n<p>    app = QApplication(sys.argv)<\/p>\n<p>    main_window = MainWindow()<\/p>\n<p>    main_window.show()<\/p>\n<p>    sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout<\/p>\n<p>class MainWindow(QWidget):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.initUI()<\/p>\n<p>    def initUI(self):<\/p>\n<p>        self.setWindowTitle(&quot;Button Click Example&quot;)<\/p>\n<p>        self.setGeometry(100, 100, 300, 200)<\/p>\n<p>        layout = QVBoxLayout()<\/p>\n<p>        self.button = QPushButton(&quot;Click Me&quot;, self)<\/p>\n<p>        self.button.clicked.connect(self.on_button_click)<\/p>\n<p>        layout.addWidget(self.button)<\/p>\n<p>        self.setLayout(layout)<\/p>\n<p>    def on_button_click(self):<\/p>\n<p>        print(&quot;Button was clicked!&quot;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    import sys<\/p>\n<p>    app = QApplication(sys.argv)<\/p>\n<p>    main_window = MainWindow()<\/p>\n<p>    main_window.show()<\/p>\n<p>    sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>Kivy\u5b9e\u73b0\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\uff1a<\/strong><\/h3>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Python\u5e93\uff0c\u7528\u4e8e\u5f00\u53d1\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\uff0c\u652f\u6301\u8de8\u5e73\u53f0\u3002\u4e0b\u9762\u662f\u4f7f\u7528Kivy\u5b9e\u73b0\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Kivy\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Kivy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install kivy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bfc\u5165Kivy\u5e93<\/h3>\n<\/p>\n<p><p>\u5bfc\u5165Kivy\u5e93\u4e2d\u7684\u5fc5\u8981\u6a21\u5757\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.boxlayout import BoxLayout<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u4e3b\u5e94\u7528\u7a0b\u5e8f\u7c7b<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7ee7\u627f\u81eaApp\u7684\u4e3b\u5e94\u7528\u7a0b\u5e8f\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        layout = BoxLayout(padding=10)<\/p>\n<p>        button = Button(text=&quot;Click Me&quot;)<\/p>\n<p>        button.bind(on_press=self.on_button_click)<\/p>\n<p>        layout.add_widget(button)<\/p>\n<p>        return layout<\/p>\n<p>    def on_button_click(self, instance):<\/p>\n<p>        print(&quot;Button was clicked!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u521b\u5efaMyApp\u5b9e\u4f8b\u5e76\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/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.boxlayout import BoxLayout<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        layout = BoxLayout(padding=10)<\/p>\n<p>        button = Button(text=&quot;Click Me&quot;)<\/p>\n<p>        button.bind(on_press=self.on_button_click)<\/p>\n<p>        layout.add_widget(button)<\/p>\n<p>        return layout<\/p>\n<p>    def on_button_click(self, instance):<\/p>\n<p>        print(&quot;Button was clicked!&quot;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u603b\u7ed3<\/strong><\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u83b7\u53d6\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4f7f\u7528Tkinter\u3001PyQt\u3001Kivy\u7b49GUI\u5e93\u90fd\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002<strong>Tkinter\u9002\u5408\u7b80\u5355\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\uff0cPyQt\u9002\u5408\u590d\u6742\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\uff0cKivy\u9002\u5408\u8de8\u5e73\u53f0\u548c\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u3002<\/strong>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h3><strong>\u6df1\u5165\u7406\u89e3\u4e8b\u4ef6\u5904\u7406\u673a\u5236<\/strong><\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cdGUI\u5e93\uff0c\u4e8b\u4ef6\u5904\u7406\u673a\u5236\u90fd\u662f\u6838\u5fc3\u3002\u4e8b\u4ef6\u5904\u7406\u673a\u5236\u5305\u62ec\u4e8b\u4ef6\u7684\u89e6\u53d1\u3001\u4e8b\u4ef6\u7684\u6355\u83b7\u548c\u4e8b\u4ef6\u7684\u54cd\u5e94\u3002\u5728\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u5904\u7406\u4e2d\uff0c\u6309\u94ae\u5355\u51fb\u662f\u4e8b\u4ef6\u7684\u89e6\u53d1\uff0c\u7ed1\u5b9a\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u662f\u4e8b\u4ef6\u7684\u6355\u83b7\u548c\u54cd\u5e94\u3002\u7406\u89e3\u4e8b\u4ef6\u5904\u7406\u673a\u5236\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u8bbe\u8ba1\u548c\u5b9e\u73b0\u590d\u6742\u7684\u4ea4\u4e92\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3><strong>\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u5904\u7406\u51fd\u6570<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u5f80\u5f80\u9700\u8981\u5904\u7406\u66f4\u591a\u7684\u903b\u8f91\uff0c\u6bd4\u5982\u66f4\u65b0\u754c\u9762\u72b6\u6001\u3001\u4e0e\u540e\u53f0\u6570\u636e\u4ea4\u4e92\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\u8c03\u7528\u5176\u4ed6\u51fd\u6570\u6216\u65b9\u6cd5\uff0c\u5b9e\u73b0\u590d\u6742\u7684\u903b\u8f91\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u5728\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\u66f4\u65b0\u6807\u7b7e\u7684\u6587\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_button_click():<\/p>\n<p>    label.config(text=&quot;Button was clicked!&quot;)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Button Click Example&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<p>label = tk.Label(root, text=&quot;Hello, World!&quot;)<\/p>\n<p>label.pack(pady=20)<\/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>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u5904\u7406\u591a\u6309\u94ae\u4e8b\u4ef6<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e2a\u754c\u9762\u4e2d\u53ef\u80fd\u6709\u591a\u4e2a\u6309\u94ae\uff0c\u6bcf\u4e2a\u6309\u94ae\u90fd\u6709\u4e0d\u540c\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7\u4e3a\u6bcf\u4e2a\u6309\u94ae\u7ed1\u5b9a\u4e0d\u540c\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u6216\u8005\u5728\u540c\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\u533a\u5206\u4e0d\u540c\u7684\u6309\u94ae\uff0c\u5b9e\u73b0\u591a\u6309\u94ae\u4e8b\u4ef6\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5728PyQt\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u6309\u94ae\u5bf9\u8c61\u6765\u533a\u5206\u4e0d\u540c\u7684\u6309\u94ae\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout<\/p>\n<p>class MainWindow(QWidget):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.initUI()<\/p>\n<p>    def initUI(self):<\/p>\n<p>        self.setWindowTitle(&quot;Button Click Example&quot;)<\/p>\n<p>        self.setGeometry(100, 100, 300, 200)<\/p>\n<p>        layout = QVBoxLayout()<\/p>\n<p>        self.button1 = QPushButton(&quot;Button 1&quot;, self)<\/p>\n<p>        self.button1.clicked.connect(lambda: self.on_button_click(self.button1))<\/p>\n<p>        self.button2 = QPushButton(&quot;Button 2&quot;, self)<\/p>\n<p>        self.button2.clicked.connect(lambda: self.on_button_click(self.button2))<\/p>\n<p>        layout.addWidget(self.button1)<\/p>\n<p>        layout.addWidget(self.button2)<\/p>\n<p>        self.setLayout(layout)<\/p>\n<p>    def on_button_click(self, button):<\/p>\n<p>        if button == self.button1:<\/p>\n<p>            print(&quot;Button 1 was clicked!&quot;)<\/p>\n<p>        elif button == self.button2:<\/p>\n<p>            print(&quot;Button 2 was clicked!&quot;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    import sys<\/p>\n<p>    app = QApplication(sys.argv)<\/p>\n<p>    main_window = MainWindow()<\/p>\n<p>    main_window.show()<\/p>\n<p>    sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u4f7f\u7528Lambda\u51fd\u6570\u7b80\u5316\u4e8b\u4ef6\u7ed1\u5b9a<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u7ed1\u5b9a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Lambda\u51fd\u6570\u7b80\u5316\u4ee3\u7801\u3002Lambda\u51fd\u6570\u662f\u4e00\u79cd\u533f\u540d\u51fd\u6570\uff0c\u5e38\u7528\u4e8e\u7b80\u5316\u7b80\u5355\u7684\u4e8b\u4ef6\u5904\u7406\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u5728Kivy\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Lambda\u51fd\u6570\u7b80\u5316\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u7684\u7ed1\u5b9a\uff1a<\/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.boxlayout import BoxLayout<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        layout = BoxLayout(padding=10)<\/p>\n<p>        button1 = Button(text=&quot;Button 1&quot;)<\/p>\n<p>        button1.bind(on_press=lambda instance: self.on_button_click(instance, &quot;Button 1&quot;))<\/p>\n<p>        button2 = Button(text=&quot;Button 2&quot;)<\/p>\n<p>        button2.bind(on_press=lambda instance: self.on_button_click(instance, &quot;Button 2&quot;))<\/p>\n<p>        layout.add_widget(button1)<\/p>\n<p>        layout.add_widget(button2)<\/p>\n<p>        return layout<\/p>\n<p>    def on_button_click(self, instance, button_name):<\/p>\n<p>        print(f&quot;{button_name} was clicked!&quot;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u603b\u7ed3\u4e0e\u5efa\u8bae<\/strong><\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cdGUI\u5e93\uff0c\u7406\u89e3\u4e8b\u4ef6\u5904\u7406\u673a\u5236\u548c\u7075\u6d3b\u4f7f\u7528\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u662f\u5b9e\u73b0\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u7684\u5173\u952e\u3002<strong>\u901a\u8fc7\u5408\u7406\u8bbe\u8ba1\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u4ea4\u4e92\u903b\u8f91\uff0c\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u3002<\/strong>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684GUI\u5e93\uff0c\u5e76\u7075\u6d3b\u8fd0\u7528\u4e8b\u4ef6\u5904\u7406\u6280\u672f\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u5e94\u7528\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\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\u5355\u51fb\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\u5305\u62ec\u6309\u94ae\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u5e76\u5c06\u5176\u7ed1\u5b9a\u5230\u6309\u94ae\u7684\u5355\u51fb\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\ndef on_button_click():\n    print(&quot;\u6309\u94ae\u88ab\u5355\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>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7a97\u53e3\u5e76\u6dfb\u52a0\u4e00\u4e2a\u6309\u94ae\uff0c\u5355\u51fb\u6309\u94ae\u540e\u4f1a\u89e6\u53d1<code>on_button_click<\/code>\u51fd\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u591a\u4e2a\u6309\u94ae\u7684\u5355\u51fb\u4e8b\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5904\u7406\u591a\u4e2a\u6309\u94ae\u7684\u5355\u51fb\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6309\u94ae\u5b9a\u4e49\u4e0d\u540c\u7684\u56de\u8c03\u51fd\u6570\uff0c\u6216\u8005\u4f7f\u7528\u4e00\u4e2a\u901a\u7528\u7684\u51fd\u6570\uff0c\u5e76\u901a\u8fc7\u53c2\u6570\u533a\u5206\u6309\u94ae\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\ndef on_button_click(button_name):\n    print(f&quot;{button_name} \u88ab\u5355\u51fb!&quot;)\n\nroot = tk.Tk()\nbutton1 = tk.Button(root, text=&quot;\u6309\u94ae1&quot;, command=lambda: on_button_click(&quot;\u6309\u94ae1&quot;))\nbutton1.pack()\n\nbutton2 = tk.Button(root, text=&quot;\u6309\u94ae2&quot;, command=lambda: on_button_click(&quot;\u6309\u94ae2&quot;))\nbutton2.pack()\n\nroot.mainloop()\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>on_button_click<\/code>\u51fd\u6570\u6839\u636e\u4f20\u5165\u7684\u53c2\u6570\u5224\u65ad\u54ea\u4e2a\u6309\u94ae\u88ab\u5355\u51fb\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u4e2d\u66f4\u65b0\u754c\u9762\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u6309\u94ae\u7684\u5355\u51fb\u4e8b\u4ef6\u6765\u66f4\u65b0\u754c\u9762\u4e0a\u7684\u5176\u4ed6\u5143\u7d20\uff0c\u4f8b\u5982\u6807\u7b7e\u6216\u6587\u672c\u6846\u3002\u4f7f\u7528Tkinter\uff0c\u60a8\u53ea\u9700\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u64cd\u4f5c\u8fd9\u4e9b\u7ec4\u4ef6\u5373\u53ef\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\ndef update_label():\n    label.config(text=&quot;\u6309\u94ae\u5df2\u5355\u51fb!&quot;)\n\nroot = tk.Tk()\nlabel = tk.Label(root, text=&quot;\u8bf7\u5355\u51fb\u6309\u94ae&quot;)\nlabel.pack()\n\nbutton = tk.Button(root, text=&quot;\u70b9\u51fb\u6211&quot;, command=update_label)\nbutton.pack()\n\nroot.mainloop()\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6309\u94ae\u88ab\u5355\u51fb\u540e\uff0c\u6807\u7b7e\u7684\u6587\u672c\u4f1a\u66f4\u65b0\u4e3a\u201c\u6309\u94ae\u5df2\u5355\u51fb!\u201d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u83b7\u53d6Python\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Tkinter\u3001PyQt\u3001Kivy\u7b49\u6846\u67b6\u3002\u5728\u8fd9\u4e9b\u6846\u67b6\u4e2d\uff0c\u6700\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1179981,"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\/1179972"}],"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=1179972"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179972\/revisions"}],"predecessor-version":[{"id":1179983,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179972\/revisions\/1179983"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179981"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}