{"id":1084689,"date":"2025-01-08T13:09:30","date_gmt":"2025-01-08T05:09:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1084689.html"},"modified":"2025-01-08T13:09:32","modified_gmt":"2025-01-08T05:09:32","slug":"python%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e6%96%87%e5%ad%97%e6%8c%89%e9%92%ae-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1084689.html","title":{"rendered":"python\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u6587\u5b57\u6309\u94ae"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194926\/40d8e86f-42f6-4509-8ebc-f64542001783.webp\" alt=\"python\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u6587\u5b57\u6309\u94ae\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u6587\u5b57\u6309\u94ae\u3001\u5305\u62ec\u4f7f\u7528Tkinter\u5e93\u3001PyQt\u5e93\u6216Kivy\u5e93\u7b49\u3002\u8fd9\u91cc\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tkinter\u5e93\u521b\u5efa\u4e00\u4e2a\u6587\u5b57\u6309\u94ae\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684Tkinter\u5e93\u662f\u4e00\u4e2a\u6807\u51c6GUI\uff08\u56fe\u5f62\u7528\u6237\u754c\u9762\uff09\u5e93\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u6b65\u6b65\u521b\u5efa\u4e00\u4e2a\u6587\u5b57\u6309\u94ae\u7684\u8be6\u7ec6\u8fc7\u7a0b\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u5b89\u88c5Tkinter\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002\u53ea\u9700\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u5df2\u7ecf\u5b89\u88c5\u4e86Tkinter\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Tkinter\u7a97\u53e3<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6309\u94ae\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Tkinter\u7a97\u53e3\u3002\u4ee5\u4e0b\u662f\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;Tkinter Button Example&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u7a97\u53e3\u7684\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>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u7a97\u53e3\uff0c\u6807\u9898\u4e3a\u201cTkinter Button Example\u201d\uff0c\u5927\u5c0f\u4e3a300&#215;200\u50cf\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u521b\u5efa\u4e00\u4e2a\u6587\u5b57\u6309\u94ae<\/strong><\/p>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6587\u5b57\u6309\u94ae\u3002\u6211\u4eec\u5c06\u4f7f\u7528Tkinter\u7684<code>Button<\/code>\u5c0f\u90e8\u4ef6\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4ee5\u4e0b\u662f\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;Tkinter Button Example&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6587\u5b57\u6309\u94ae<\/strong><\/h2>\n<p>button = tk.Button(root, text=&quot;Click Me&quot;, command=lambda: print(&quot;Button Clicked&quot;))<\/p>\n<h2><strong>\u5c06\u6309\u94ae\u653e\u7f6e\u5728\u7a97\u53e3\u4e2d<\/strong><\/h2>\n<p>button.pack(pady=20)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u7a97\u53e3\u7684\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u6587\u5b57\u6309\u94ae\uff0c\u6309\u94ae\u4e0a\u663e\u793a\u201cClick Me\u201d\u3002\u6309\u94ae\u7684<code>command<\/code>\u53c2\u6570\u6307\u5b9a\u4e86\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\u6267\u884c\u7684\u51fd\u6570\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u7b80\u5355\u7684lambda\u51fd\u6570\u6765\u6253\u5370\u201cButton Clicked\u201d\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u8be6\u7ec6\u63cf\u8ff0\u6309\u94ae\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>Tkinter\u7684<code>Button<\/code>\u5c0f\u90e8\u4ef6\u6709\u8bb8\u591a\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u5b9a\u5236\u6309\u94ae\u7684\u5916\u89c2\u548c\u884c\u4e3a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>text<\/strong>\uff1a\u6309\u94ae\u4e0a\u663e\u793a\u7684\u6587\u5b57\u3002<\/li>\n<li><strong>command<\/strong>\uff1a\u6309\u94ae\u70b9\u51fb\u65f6\u6267\u884c\u7684\u51fd\u6570\u3002<\/li>\n<li><strong>width<\/strong> \u548c <strong>height<\/strong>\uff1a\u6309\u94ae\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u3002<\/li>\n<li><strong>bg<\/strong> \u548c <strong>fg<\/strong>\uff1a\u6309\u94ae\u7684\u80cc\u666f\u8272\u548c\u524d\u666f\u8272\uff08\u6587\u5b57\u989c\u8272\uff09\u3002<\/li>\n<li><strong>font<\/strong>\uff1a\u6309\u94ae\u6587\u5b57\u7684\u5b57\u4f53\u548c\u5927\u5c0f\u3002<\/li>\n<li><strong>relief<\/strong>\uff1a\u6309\u94ae\u7684\u8fb9\u6846\u6837\u5f0f\uff0c\u53ef\u4ee5\u662f<code>flat<\/code>\u3001<code>raised<\/code>\u3001<code>sunken<\/code>\u3001<code>groove<\/code>\u6216<code>ridge<\/code>\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u66f4\u8be6\u7ec6\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5c5e\u6027\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;Tkinter Button Example&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6587\u5b57\u6309\u94ae\uff0c\u8bbe\u7f6e\u5404\u79cd\u5c5e\u6027<\/strong><\/h2>\n<p>button = tk.Button(root, <\/p>\n<p>                   text=&quot;Click Me&quot;, <\/p>\n<p>                   command=lambda: print(&quot;Button Clicked&quot;), <\/p>\n<p>                   width=20, <\/p>\n<p>                   height=2, <\/p>\n<p>                   bg=&quot;blue&quot;, <\/p>\n<p>                   fg=&quot;white&quot;, <\/p>\n<p>                   font=(&quot;Helvetica&quot;, 14), <\/p>\n<p>                   relief=&quot;raised&quot;)<\/p>\n<h2><strong>\u5c06\u6309\u94ae\u653e\u7f6e\u5728\u7a97\u53e3\u4e2d<\/strong><\/h2>\n<p>button.pack(pady=20)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u7a97\u53e3\u7684\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u6309\u94ae\uff0c\u8bbe\u7f6e\u4e86\u6309\u94ae\u7684\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u3001\u80cc\u666f\u8272\u3001\u524d\u666f\u8272\u3001\u5b57\u4f53\u548c\u8fb9\u6846\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u5904\u7406\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u901a\u5e38\u4f1a\u6267\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\u6765\u5904\u7406\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u3002\u4ee5\u4e0b\u662f\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;Tkinter Button Example&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u5b9a\u4e49\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u7684\u5904\u7406\u51fd\u6570<\/strong><\/h2>\n<p>def on_button_click():<\/p>\n<p>    print(&quot;Button Clicked&quot;)<\/p>\n<p>    # \u6267\u884c\u5176\u4ed6\u64cd\u4f5c<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6587\u5b57\u6309\u94ae\uff0c\u8bbe\u7f6ecommand\u5c5e\u6027\u4e3a\u5904\u7406\u51fd\u6570<\/strong><\/h2>\n<p>button = tk.Button(root, <\/p>\n<p>                   text=&quot;Click Me&quot;, <\/p>\n<p>                   command=on_button_click, <\/p>\n<p>                   width=20, <\/p>\n<p>                   height=2, <\/p>\n<p>                   bg=&quot;blue&quot;, <\/p>\n<p>                   fg=&quot;white&quot;, <\/p>\n<p>                   font=(&quot;Helvetica&quot;, 14), <\/p>\n<p>                   relief=&quot;raised&quot;)<\/p>\n<h2><strong>\u5c06\u6309\u94ae\u653e\u7f6e\u5728\u7a97\u53e3\u4e2d<\/strong><\/h2>\n<p>button.pack(pady=20)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u7a97\u53e3\u7684\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>on_button_click<\/code>\u7684\u51fd\u6570\u6765\u5904\u7406\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u3002\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c<code>on_button_click<\/code>\u51fd\u6570\u5c06\u88ab\u8c03\u7528\u5e76\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>\u516d\u3001\u4f7f\u7528\u5176\u4ed6GUI\u5e93\u521b\u5efa\u6309\u94ae<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86Tkinter\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6GUI\u5e93\uff0c\u5982PyQt\u548cKivy\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PyQt\u548cKivy\u521b\u5efa\u6309\u94ae\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528PyQt\u521b\u5efa\u6309\u94ae<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\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><p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aPyQt\u6309\u94ae\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QVBoxLayout<\/p>\n<p>import sys<\/p>\n<p>def on_button_click():<\/p>\n<p>    print(&quot;Button Clicked&quot;)<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&quot;PyQt Button Example&quot;)<\/p>\n<p>window.setGeometry(100, 100, 300, 200)<\/p>\n<p>layout = QVBoxLayout()<\/p>\n<p>button = QPushButton(&quot;Click Me&quot;)<\/p>\n<p>button.clicked.connect(on_button_click)<\/p>\n<p>layout.addWidget(button)<\/p>\n<p>window.setLayout(layout)<\/p>\n<p>window.show()<\/p>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528PyQt\u521b\u5efa\u4e86\u4e00\u4e2a\u6309\u94ae\uff0c\u5e76\u8fde\u63a5\u4e86\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528Kivy\u521b\u5efa\u6309\u94ae<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Kivy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install kivy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aKivy\u6309\u94ae\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;, font_size=20)<\/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 Clicked&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Kivy\u521b\u5efa\u4e86\u4e00\u4e2a\u6309\u94ae\uff0c\u5e76\u7ed1\u5b9a\u4e86\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u7684Tkinter\u5e93\u521b\u5efa\u4e00\u4e2a\u6587\u5b57\u6309\u94ae\uff0c\u5e76\u4ecb\u7ecd\u4e86\u6309\u94ae\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u6211\u4eec\u8fd8\u5c55\u793a\u4e86\u5982\u4f55\u5904\u7406\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\uff0c\u5e76\u7b80\u8981\u4ecb\u7ecd\u4e86\u4f7f\u7528\u5176\u4ed6GUI\u5e93\uff08\u5982PyQt\u548cKivy\uff09\u521b\u5efa\u6309\u94ae\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u5728Python\u4e2d\u521b\u5efa\u66f4\u4e30\u5bcc\u7684\u56fe\u5f62\u7528\u6237\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\u6587\u5b57\u6309\u94ae\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u521b\u5efa\u6587\u5b57\u6309\u94ae\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u7684\u73af\u5883\u4e2d\u5b89\u88c5\u4e86Tkinter\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u7a97\u53e3\uff0c\u5e76\u5728\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6587\u5b57\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&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>\u4e0a\u8ff0\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u6587\u5b57\u6309\u94ae\u7684\u7b80\u5355GUI\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5176\u4ed6\u5e93\u6765\u521b\u5efa\u6587\u5b57\u6309\u94ae\uff1f<\/strong><br \/>\u9664\u4e86Tkinter\uff0c\u60a8\u8fd8\u53ef\u4ee5\u9009\u62e9\u5176\u4ed6GUI\u5e93\uff0c\u5982PyQt\u3001Kivy\u6216wxPython\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u521b\u5efa\u66f4\u590d\u6742\u7684\u754c\u9762\u548c\u6309\u94ae\u3002\u4f8b\u5982\uff0c\u4f7f\u7528PyQt\u53ef\u4ee5\u5b9e\u73b0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QPushButton, QWidget\n\napp = QApplication([])\nwindow = QWidget()\nbutton = QPushButton(&quot;\u70b9\u51fb\u6211&quot;, window)\nbutton.clicked.connect(lambda: print(&quot;\u6309\u94ae\u88ab\u70b9\u51fb\u4e86&quot;))\nwindow.show()\napp.exec_()\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u66f4\u9ad8\u81ea\u5b9a\u4e49\u6216\u8de8\u5e73\u53f0\u652f\u6301\u7684\u5e94\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u6309\u94ae\u7684\u5916\u89c2\u548c\u98ce\u683c\uff1f<\/strong><br \/>\u81ea\u5b9a\u4e49\u6309\u94ae\u7684\u5916\u89c2\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6309\u94ae\u7684\u6837\u5f0f\u9009\u9879\u6765\u5b9e\u73b0\u3002\u5728Tkinter\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>fg<\/code>\u548c<code>bg<\/code>\u53c2\u6570\u6765\u6539\u53d8\u6309\u94ae\u7684\u524d\u666f\u8272\u548c\u80cc\u666f\u8272\u3002\u6bd4\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">button = tk.Button(root, text=&quot;\u70b9\u51fb\u6211&quot;, command=on_button_click, fg=&quot;white&quot;, bg=&quot;blue&quot;)\n<\/code><\/pre>\n<p>\u5728PyQt\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6837\u5f0f\u8868\u6765\u5b9a\u4e49\u6309\u94ae\u7684\u5916\u89c2\uff1a<\/p>\n<pre><code class=\"language-python\">button.setStyleSheet(&quot;background-color: blue; color: white;&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u9009\u9879\u4f7f\u60a8\u80fd\u591f\u521b\u5efa\u66f4\u5438\u5f15\u4eba\u7684\u7528\u6237\u754c\u9762\uff0c\u7b26\u5408\u60a8\u5e94\u7528\u7684\u6574\u4f53\u8bbe\u8ba1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u6587\u5b57\u6309\u94ae\u3001\u5305\u62ec\u4f7f\u7528Tkinter\u5e93\u3001PyQt\u5e93\u6216Kivy\u5e93\u7b49\u3002\u8fd9\u91cc\u6211\u4eec\u5c06\u8be6 [&hellip;]","protected":false},"author":3,"featured_media":1084694,"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\/1084689"}],"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=1084689"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084689\/revisions"}],"predecessor-version":[{"id":1084696,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084689\/revisions\/1084696"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1084694"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1084689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1084689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1084689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}