{"id":1121327,"date":"2025-01-08T19:08:07","date_gmt":"2025-01-08T11:08:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1121327.html"},"modified":"2025-01-08T19:08:10","modified_gmt":"2025-01-08T11:08:10","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e6%8c%89%e9%92%ae%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%8a%e6%8b%89","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1121327.html","title":{"rendered":"python\u5982\u4f55\u5728\u6309\u94ae\u4e0a\u8bbe\u7f6e\u4e0a\u62c9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083616\/394f143e-55fa-414c-b8c9-8f69a590b9f6.webp\" alt=\"python\u5982\u4f55\u5728\u6309\u94ae\u4e0a\u8bbe\u7f6e\u4e0a\u62c9\" \/><\/p>\n<h2><strong>Python\u5982\u4f55\u5728\u6309\u94ae\u4e0a\u8bbe\u7f6e\u4e0a\u62c9<\/strong><\/h2>\n<p><p><strong>\u5728Python\u4e2d\u8bbe\u7f6e\u6309\u94ae\u4e0a\u62c9\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Tkinter\u5e93\u3001\u4f7f\u7528Qt\u5e93\u3001\u4f7f\u7528Kivy\u5e93<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u7b2c\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528Tkinter\u5e93\u6765\u8bbe\u7f6e\u6309\u94ae\u4e0a\u62c9\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u793a\u4f8b\u4ee3\u7801\u548c\u64cd\u4f5c\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528Tkinter\u5e93\u8bbe\u7f6e\u6309\u94ae\u4e0a\u62c9<\/h2>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002\u901a\u8fc7\u4f7f\u7528Tkinter\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u6309\u94ae\u4e0a\u5b9e\u73b0\u4e0a\u62c9\u83dc\u5355\u3002\u4e0b\u9762\u662f\u5177\u4f53\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>1. Tkinter\u5e93\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e00\u7ec4\u7528\u4e8e\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u7684\u5de5\u5177\u3002Tkinter\u5e93\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u5b66\uff0c\u4e14\u4e0d\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u8f6f\u4ef6\u5305\u3002<\/p>\n<\/p>\n<p><h3>2. \u5b89\u88c5Tkinter\u5e93<\/h3>\n<\/p>\n<p><p>Tkinter\u901a\u5e38\u968fPython\u5b89\u88c5\u5305\u4e00\u8d77\u5b89\u88c5\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5Tkinter\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\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><h3>3. \u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Tkinter\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Tkinter\u7a97\u53e3\uff0c\u5e76\u5728\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6309\u94ae\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\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\u4e00\u4e2aTkinter\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter Button Example&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u5b9a\u4e49\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6<\/strong><\/h2>\n<p>def on_button_click():<\/p>\n<p>    print(&quot;Button Clicked!&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6309\u94ae<\/strong><\/h2>\n<p>button = tk.Button(root, text=&quot;Click Me&quot;, command=on_button_click)<\/p>\n<p>button.pack(pady=20)<\/p>\n<h2><strong>\u8fd0\u884cTkinter\u4e8b\u4ef6\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><h3>4. \u5728\u6309\u94ae\u4e0a\u8bbe\u7f6e\u4e0a\u62c9\u83dc\u5355<\/h3>\n<\/p>\n<p><p>\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tk.Menu<\/code>\u7c7b\u6765\u521b\u5efa\u83dc\u5355\u3002\u4e3a\u4e86\u5728\u6309\u94ae\u4e0a\u8bbe\u7f6e\u4e0a\u62c9\u83dc\u5355\uff0c\u6211\u4eec\u9700\u8981\u5728\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u4e2d\u663e\u793a\u83dc\u5355\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u4e0a\u62c9\u83dc\u5355\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\u4e00\u4e2aTkinter\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter Button Example&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u5b9a\u4e49\u4e0a\u62c9\u83dc\u5355<\/strong><\/h2>\n<p>def create_popup_menu(event):<\/p>\n<p>    popup_menu = tk.Menu(root, tearoff=0)<\/p>\n<p>    popup_menu.add_command(label=&quot;Option 1&quot;, command=lambda: print(&quot;Option 1 Selected&quot;))<\/p>\n<p>    popup_menu.add_command(label=&quot;Option 2&quot;, command=lambda: print(&quot;Option 2 Selected&quot;))<\/p>\n<p>    popup_menu.add_command(label=&quot;Option 3&quot;, command=lambda: print(&quot;Option 3 Selected&quot;))<\/p>\n<p>    popup_menu.post(event.x_root, event.y_root)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6309\u94ae<\/strong><\/h2>\n<p>button = tk.Button(root, text=&quot;Click Me&quot;)<\/p>\n<p>button.pack(pady=20)<\/p>\n<h2><strong>\u7ed1\u5b9a\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u4ee5\u663e\u793a\u4e0a\u62c9\u83dc\u5355<\/strong><\/h2>\n<p>button.bind(&quot;&lt;Button-1&gt;&quot;, create_popup_menu)<\/p>\n<h2><strong>\u8fd0\u884cTkinter\u4e8b\u4ef6\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>create_popup_menu<\/code>\u51fd\u6570\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e0a\u62c9\u83dc\u5355\uff0c\u5e76\u5728\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u4e2d\u663e\u793a\u8be5\u83dc\u5355\u3002\u4f7f\u7528<code>bind<\/code>\u65b9\u6cd5\u5c06\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u4e0e<code>create_popup_menu<\/code>\u51fd\u6570\u7ed1\u5b9a\uff0c\u4ece\u800c\u5b9e\u73b0\u6309\u94ae\u4e0a\u7684\u4e0a\u62c9\u83dc\u5355\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>5. \u81ea\u5b9a\u4e49\u4e0a\u62c9\u83dc\u5355\u9009\u9879<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\u4e0a\u62c9\u83dc\u5355\u7684\u9009\u9879\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5305\u542b\u66f4\u591a\u9009\u9879\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\u4e00\u4e2aTkinter\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter Button Example&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u5b9a\u4e49\u4e0a\u62c9\u83dc\u5355<\/strong><\/h2>\n<p>def create_popup_menu(event):<\/p>\n<p>    popup_menu = tk.Menu(root, tearoff=0)<\/p>\n<p>    popup_menu.add_command(label=&quot;Option 1&quot;, command=lambda: print(&quot;Option 1 Selected&quot;))<\/p>\n<p>    popup_menu.add_command(label=&quot;Option 2&quot;, command=lambda: print(&quot;Option 2 Selected&quot;))<\/p>\n<p>    popup_menu.add_command(label=&quot;Option 3&quot;, command=lambda: print(&quot;Option 3 Selected&quot;))<\/p>\n<p>    popup_menu.add_separator()  # \u6dfb\u52a0\u5206\u9694\u7ebf<\/p>\n<p>    popup_menu.add_command(label=&quot;Exit&quot;, command=root.quit)<\/p>\n<p>    popup_menu.post(event.x_root, event.y_root)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6309\u94ae<\/strong><\/h2>\n<p>button = tk.Button(root, text=&quot;Click Me&quot;)<\/p>\n<p>button.pack(pady=20)<\/p>\n<h2><strong>\u7ed1\u5b9a\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u4ee5\u663e\u793a\u4e0a\u62c9\u83dc\u5355<\/strong><\/h2>\n<p>button.bind(&quot;&lt;Button-1&gt;&quot;, create_popup_menu)<\/p>\n<h2><strong>\u8fd0\u884cTkinter\u4e8b\u4ef6\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\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5206\u9694\u7ebf\u548c\u4e00\u4e2a\u201cExit\u201d\u9009\u9879\uff0c\u70b9\u51fb\u201cExit\u201d\u9009\u9879\u53ef\u4ee5\u5173\u95ed\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528Qt\u5e93\u8bbe\u7f6e\u6309\u94ae\u4e0a\u62c9<\/h2>\n<\/p>\n<p><p>\u9664\u4e86Tkinter\u5e93\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528Qt\u5e93\u6765\u5b9e\u73b0\u6309\u94ae\u4e0a\u7684\u4e0a\u62c9\u83dc\u5355\u3002Qt\u5e93\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u6784\u5efa\u590d\u6742\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002\u4e0b\u9762\u662f\u5177\u4f53\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>1. Qt\u5e93\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>Qt\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684C++\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684GUI\u7ec4\u4ef6\u3002PyQt\u662fQt\u5e93\u7684Python\u7ed1\u5b9a\uff0c\u5141\u8bb8\u6211\u4eec\u4f7f\u7528Python\u7f16\u5199Qt\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>2. \u5b89\u88c5PyQt\u5e93<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\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><h3>3. \u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684PyQt\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684PyQt\u7a97\u53e3\uff0c\u5e76\u5728\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6309\u94ae\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton<\/p>\n<h2><strong>\u5b9a\u4e49\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6<\/strong><\/h2>\n<p>def on_button_click():<\/p>\n<p>    print(&quot;Button Clicked!&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684PyQt\u7a97\u53e3<\/strong><\/h2>\n<p>app = QApplication([])<\/p>\n<p>window = QMainWindow()<\/p>\n<p>window.setWindowTitle(&quot;PyQt Button Example&quot;)<\/p>\n<p>window.setGeometry(100, 100, 300, 200)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6309\u94ae<\/strong><\/h2>\n<p>button = QPushButton(&quot;Click Me&quot;, window)<\/p>\n<p>button.setGeometry(100, 80, 100, 30)<\/p>\n<p>button.clicked.connect(on_button_click)<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>window.show()<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u5728\u6309\u94ae\u4e0a\u8bbe\u7f6e\u4e0a\u62c9\u83dc\u5355<\/h3>\n<\/p>\n<p><p>\u5728PyQt\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>QMenu<\/code>\u7c7b\u6765\u521b\u5efa\u83dc\u5355\u3002\u4e3a\u4e86\u5728\u6309\u94ae\u4e0a\u8bbe\u7f6e\u4e0a\u62c9\u83dc\u5355\uff0c\u6211\u4eec\u9700\u8981\u5728\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u4e2d\u663e\u793a\u83dc\u5355\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u4e0a\u62c9\u83dc\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QMenu<\/p>\n<h2><strong>\u5b9a\u4e49\u4e0a\u62c9\u83dc\u5355<\/strong><\/h2>\n<p>def create_popup_menu():<\/p>\n<p>    popup_menu = QMenu()<\/p>\n<p>    popup_menu.addAction(&quot;Option 1&quot;, lambda: print(&quot;Option 1 Selected&quot;))<\/p>\n<p>    popup_menu.addAction(&quot;Option 2&quot;, lambda: print(&quot;Option 2 Selected&quot;))<\/p>\n<p>    popup_menu.addAction(&quot;Option 3&quot;, lambda: print(&quot;Option 3 Selected&quot;))<\/p>\n<p>    popup_menu.exec_(button.mapToGlobal(button.rect().bottomLeft()))<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684PyQt\u7a97\u53e3<\/strong><\/h2>\n<p>app = QApplication([])<\/p>\n<p>window = QMainWindow()<\/p>\n<p>window.setWindowTitle(&quot;PyQt Button Example&quot;)<\/p>\n<p>window.setGeometry(100, 100, 300, 200)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6309\u94ae<\/strong><\/h2>\n<p>button = QPushButton(&quot;Click Me&quot;, window)<\/p>\n<p>button.setGeometry(100, 80, 100, 30)<\/p>\n<p>button.clicked.connect(create_popup_menu)<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>window.show()<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>create_popup_menu<\/code>\u51fd\u6570\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e0a\u62c9\u83dc\u5355\uff0c\u5e76\u5728\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u4e2d\u663e\u793a\u8be5\u83dc\u5355\u3002\u4f7f\u7528<code>button.clicked.connect<\/code>\u65b9\u6cd5\u5c06\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u4e0e<code>create_popup_menu<\/code>\u51fd\u6570\u8fde\u63a5\uff0c\u4ece\u800c\u5b9e\u73b0\u6309\u94ae\u4e0a\u7684\u4e0a\u62c9\u83dc\u5355\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>5. \u81ea\u5b9a\u4e49\u4e0a\u62c9\u83dc\u5355\u9009\u9879<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\u4e0a\u62c9\u83dc\u5355\u7684\u9009\u9879\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5305\u542b\u66f4\u591a\u9009\u9879\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QMenu<\/p>\n<h2><strong>\u5b9a\u4e49\u4e0a\u62c9\u83dc\u5355<\/strong><\/h2>\n<p>def create_popup_menu():<\/p>\n<p>    popup_menu = QMenu()<\/p>\n<p>    popup_menu.addAction(&quot;Option 1&quot;, lambda: print(&quot;Option 1 Selected&quot;))<\/p>\n<p>    popup_menu.addAction(&quot;Option 2&quot;, lambda: print(&quot;Option 2 Selected&quot;))<\/p>\n<p>    popup_menu.addAction(&quot;Option 3&quot;, lambda: print(&quot;Option 3 Selected&quot;))<\/p>\n<p>    popup_menu.addSeparator()  # \u6dfb\u52a0\u5206\u9694\u7ebf<\/p>\n<p>    popup_menu.addAction(&quot;Exit&quot;, app.quit)<\/p>\n<p>    popup_menu.exec_(button.mapToGlobal(button.rect().bottomLeft()))<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684PyQt\u7a97\u53e3<\/strong><\/h2>\n<p>app = QApplication([])<\/p>\n<p>window = QMainWindow()<\/p>\n<p>window.setWindowTitle(&quot;PyQt Button Example&quot;)<\/p>\n<p>window.setGeometry(100, 100, 300, 200)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6309\u94ae<\/strong><\/h2>\n<p>button = QPushButton(&quot;Click Me&quot;, window)<\/p>\n<p>button.setGeometry(100, 80, 100, 30)<\/p>\n<p>button.clicked.connect(create_popup_menu)<\/p>\n<h2><strong>\u663e\u793a\u7a97\u53e3<\/strong><\/h2>\n<p>window.show()<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5206\u9694\u7ebf\u548c\u4e00\u4e2a\u201cExit\u201d\u9009\u9879\uff0c\u70b9\u51fb\u201cExit\u201d\u9009\u9879\u53ef\u4ee5\u5173\u95ed\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528Kivy\u5e93\u8bbe\u7f6e\u6309\u94ae\u4e0a\u62c9<\/h2>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Python\u5e93\uff0c\u9002\u7528\u4e8e\u5f00\u53d1\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\u3002\u901a\u8fc7\u4f7f\u7528Kivy\u5e93\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u6309\u94ae\u4e0a\u5b9e\u73b0\u4e0a\u62c9\u83dc\u5355\u3002\u4e0b\u9762\u662f\u5177\u4f53\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>1. Kivy\u5e93\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u7528\u4e8e\u5f00\u53d1\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\u7684Python\u5e93\uff0c\u652f\u6301\u8de8\u5e73\u53f0\uff0c\u53ef\u4ee5\u8fd0\u884c\u5728Windows\u3001macOS\u3001Linux\u3001iOS\u548cAndroid\u4e0a\u3002<\/p>\n<\/p>\n<p><h3>2. \u5b89\u88c5Kivy\u5e93<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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><h3>3. \u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Kivy\u5e94\u7528\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Kivy\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6309\u94ae\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\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<h2><strong>\u5b9a\u4e49\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6<\/strong><\/h2>\n<p>def on_button_click(instance):<\/p>\n<p>    print(&quot;Button Clicked!&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Kivy\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\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=on_button_click)<\/p>\n<p>        layout.add_widget(button)<\/p>\n<p>        return layout<\/p>\n<h2><strong>\u8fd0\u884cKivy\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u5728\u6309\u94ae\u4e0a\u8bbe\u7f6e\u4e0a\u62c9\u83dc\u5355<\/h3>\n<\/p>\n<p><p>\u5728Kivy\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Popup<\/code>\u7c7b\u6765\u521b\u5efa\u4e0a\u62c9\u83dc\u5355\u3002\u4e3a\u4e86\u5728\u6309\u94ae\u4e0a\u8bbe\u7f6e\u4e0a\u62c9\u83dc\u5355\uff0c\u6211\u4eec\u9700\u8981\u5728\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u4e2d\u663e\u793a\u83dc\u5355\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u4e0a\u62c9\u83dc\u5355\u7684\u793a\u4f8b\u4ee3\u7801\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>from kivy.uix.popup import Popup<\/p>\n<p>from kivy.uix.gridlayout import GridLayout<\/p>\n<p>from kivy.uix.label import Label<\/p>\n<h2><strong>\u5b9a\u4e49\u4e0a\u62c9\u83dc\u5355<\/strong><\/h2>\n<p>def create_popup_menu(instance):<\/p>\n<p>    layout = GridLayout(cols=1, padding=10)<\/p>\n<p>    layout.add_widget(Button(text=&quot;Option 1&quot;, on_press=lambda x: print(&quot;Option 1 Selected&quot;)))<\/p>\n<p>    layout.add_widget(Button(text=&quot;Option 2&quot;, on_press=lambda x: print(&quot;Option 2 Selected&quot;)))<\/p>\n<p>    layout.add_widget(Button(text=&quot;Option 3&quot;, on_press=lambda x: print(&quot;Option 3 Selected&quot;)))<\/p>\n<p>    layout.add_widget(Button(text=&quot;Close&quot;, on_press=lambda x: popup.dismiss()))<\/p>\n<p>    popup = Popup(title=&#39;Menu&#39;, content=layout, size_hint=(None, None), size=(200, 200))<\/p>\n<p>    popup.open()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Kivy\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\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=create_popup_menu)<\/p>\n<p>        layout.add_widget(button)<\/p>\n<p>        return layout<\/p>\n<h2><strong>\u8fd0\u884cKivy\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>create_popup_menu<\/code>\u51fd\u6570\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e0a\u62c9\u83dc\u5355\uff0c\u5e76\u5728\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u4e2d\u663e\u793a\u8be5\u83dc\u5355\u3002\u4f7f\u7528<code>button.bind<\/code>\u65b9\u6cd5\u5c06\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u4e0e<code>create_popup_menu<\/code>\u51fd\u6570\u7ed1\u5b9a\uff0c\u4ece\u800c\u5b9e\u73b0\u6309\u94ae\u4e0a\u7684\u4e0a\u62c9\u83dc\u5355\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>5. \u81ea\u5b9a\u4e49\u4e0a\u62c9\u83dc\u5355\u9009\u9879<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\u4e0a\u62c9\u83dc\u5355\u7684\u9009\u9879\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5305\u542b\u66f4\u591a\u9009\u9879\u7684\u793a\u4f8b\u4ee3\u7801\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>from kivy.uix.popup import Popup<\/p>\n<p>from kivy.uix.gridlayout import GridLayout<\/p>\n<p>from kivy.uix.label import Label<\/p>\n<h2><strong>\u5b9a\u4e49\u4e0a\u62c9\u83dc\u5355<\/strong><\/h2>\n<p>def create_popup_menu(instance):<\/p>\n<p>    layout = GridLayout(cols=1, padding=10)<\/p>\n<p>    layout.add_widget(Button(text=&quot;Option 1&quot;, on_press=lambda x: print(&quot;Option 1 Selected&quot;)))<\/p>\n<p>    layout.add_widget(Button(text=&quot;Option 2&quot;, on_press=lambda x: print(&quot;Option 2 Selected&quot;)))<\/p>\n<p>    layout.add_widget(Button(text=&quot;Option 3&quot;, on_press=lambda x: print(&quot;Option 3 Selected&quot;)))<\/p>\n<p>    layout.add_widget(Label(text=&quot;-----------------&quot;))  # \u6dfb\u52a0\u5206\u9694\u7ebf<\/p>\n<p>    layout.add_widget(Button(text=&quot;Exit&quot;, on_press=lambda x: App.get_running_app().stop()))<\/p>\n<p>    popup = Popup(title=&#39;Menu&#39;, content=layout, size_hint=(None, None), size=(200, 200))<\/p>\n<p>    popup.open()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Kivy\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\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=create_popup_menu)<\/p>\n<p>        layout.add_widget(button)<\/p>\n<p>        return layout<\/p>\n<h2><strong>\u8fd0\u884cKivy\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5206\u9694\u7ebf\u548c\u4e00\u4e2a\u201cExit\u201d\u9009\u9879\uff0c\u70b9\u51fb\u201cExit\u201d\u9009\u9879\u53ef\u4ee5\u5173\u95ed\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Tkinter\u5e93\u3001Qt\u5e93\u548cKivy\u5e93\u5728\u6309\u94ae\u4e0a\u8bbe\u7f6e\u4e0a\u62c9\u83dc\u5355\u7684\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u63d0\u4f9b\u4e86\u5177\u4f53\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u4f60\u80fd\u591f\u5728\u81ea\u5df1\u7684Python\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5b9e\u73b0\u66f4\u52a0\u4e30\u5bcc\u7684\u7528\u6237\u754c\u9762\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4e3a\u6309\u94ae\u6dfb\u52a0\u4e0a\u62c9\u6548\u679c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efaGUI\u5e94\u7528\u7a0b\u5e8f\u5e76\u4e3a\u6309\u94ae\u6dfb\u52a0\u4e0a\u62c9\u6548\u679c\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u521b\u5efa\u4e00\u4e2a\u6309\u94ae\u5e76\u4f7f\u7528<code>bind<\/code>\u65b9\u6cd5\u6765\u54cd\u5e94\u9f20\u6807\u4e8b\u4ef6\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\u6539\u53d8\u5176\u5916\u89c2\uff0c\u6a21\u62df\u4e0a\u62c9\u6548\u679c\u3002\u53ef\u4ee5\u4f7f\u7528<code>Button<\/code>\u7684<code>config<\/code>\u65b9\u6cd5\u6765\u6539\u53d8\u6309\u94ae\u7684\u80cc\u666f\u8272\u6216\u5176\u4ed6\u5c5e\u6027\uff0c\u4ee5\u589e\u5f3a\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<p><strong>\u5728Tkinter\u4e2d\u5982\u4f55\u5b9e\u73b0\u6309\u94ae\u7684\u52a8\u6001\u53cd\u9988\uff1f<\/strong><br \/>\u5b9e\u73b0\u6309\u94ae\u7684\u52a8\u6001\u53cd\u9988\u901a\u5e38\u6d89\u53ca\u5230\u9f20\u6807\u4e8b\u4ef6\u7684\u7ed1\u5b9a\uff0c\u5982<code>&lt;Enter&gt;<\/code>\u548c<code>&lt;Leave&gt;<\/code>\u4e8b\u4ef6\u3002\u901a\u8fc7\u8fd9\u4e9b\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u5728\u9f20\u6807\u8fdb\u5165\u548c\u79bb\u5f00\u6309\u94ae\u65f6\u6539\u53d8\u6309\u94ae\u7684\u6837\u5f0f\uff0c\u4f8b\u5982\u66f4\u6539\u989c\u8272\u6216\u5927\u5c0f\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u6309\u94ae\u4e0a\u4f7f\u7528<code>tkinter.ttk<\/code>\u6a21\u5757\u4e2d\u7684<code>Style<\/code>\u7c7b\uff0c\u6765\u5b9a\u4e49\u66f4\u52a0\u590d\u6742\u7684\u6837\u5f0f\uff0c\u4ee5\u5b9e\u73b0\u66f4\u4e30\u5bcc\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6Python GUI\u6846\u67b6\u53ef\u4ee5\u5b9e\u73b0\u6309\u94ae\u7684\u4e0a\u62c9\u6548\u679c\uff1f<\/strong><br \/>\u9664\u4e86Tkinter\uff0cPyQt\u548cKivy\u7b49\u5176\u4ed6Python GUI\u6846\u67b6\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u6309\u94ae\u7684\u4e0a\u62c9\u6548\u679c\u3002\u5728PyQt\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>QPushButton<\/code>\u7c7b\uff0c\u5e76\u901a\u8fc7\u91cd\u5199\u5176<code>enterEvent<\/code>\u548c<code>leaveEvent<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\u3002\u5728Kivy\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Button<\/code>\u7ec4\u4ef6\uff0c\u5e76\u901a\u8fc7\u89e6\u6478\u4e8b\u4ef6\u8fdb\u884c\u6837\u5f0f\u7684\u53d8\u5316\uff0c\u4ece\u800c\u5b9e\u73b0\u4e0a\u62c9\u6548\u679c\u3002\u8fd9\u4e9b\u6846\u67b6\u5404\u6709\u7279\u70b9\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u5728\u6309\u94ae\u4e0a\u8bbe\u7f6e\u4e0a\u62c9 \u5728Python\u4e2d\u8bbe\u7f6e\u6309\u94ae\u4e0a\u62c9\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Tkinter\u5e93\u3001\u4f7f\u7528Qt\u5e93\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1121335,"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\/1121327"}],"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=1121327"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121327\/revisions"}],"predecessor-version":[{"id":1121336,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121327\/revisions\/1121336"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1121335"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1121327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1121327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1121327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}