{"id":1029316,"date":"2024-12-31T11:07:09","date_gmt":"2024-12-31T03:07:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1029316.html"},"modified":"2024-12-31T11:07:12","modified_gmt":"2024-12-31T03:07:12","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python%e8%84%9a%e6%9c%ac%e5%81%9a%e6%88%90%e7%95%8c%e9%9d%a2%e5%8c%96","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1029316.html","title":{"rendered":"\u5982\u4f55\u5c06python\u811a\u672c\u505a\u6210\u754c\u9762\u5316"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/03fa5741-e817-4b48-86a9-81510dfb51c7.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5c06python\u811a\u672c\u505a\u6210\u754c\u9762\u5316\" \/><\/p>\n<p><p> <strong>\u5c06Python\u811a\u672c\u505a\u6210\u754c\u9762\u5316\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u9009\u62e9\u5408\u9002\u7684GUI\u6846\u67b6\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3001\u7f16\u5199\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3001\u7ed1\u5b9a\u4e8b\u4ef6\u5e76\u8fdb\u884c\u6d4b\u8bd5\u3002<\/strong> \u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684GUI\u6846\u67b6\u662f\u6700\u4e3a\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u4e0d\u540c\u7684\u6846\u67b6\u6709\u4e0d\u540c\u7684\u529f\u80fd\u548c\u4f7f\u7528\u96be\u5ea6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u5c06\u811a\u672c\u505a\u6210\u754c\u9762\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684GUI\u6846\u67b6<\/h3>\n<\/p>\n<p><p>Python\u6709\u591a\u4e2a\u6d41\u884c\u7684GUI\u6846\u67b6\u53ef\u4f9b\u9009\u62e9\uff0c\u5305\u62ecTkinter\u3001PyQt\u3001Kivy\u7b49\u3002\u9009\u62e9\u6846\u67b6\u65f6\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u56e0\u7d20\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Tkinter\uff1a<\/strong> \u81ea\u5e26\u4e8ePython\u6807\u51c6\u5e93\uff0c\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li><strong>PyQt\uff1a<\/strong> \u529f\u80fd\u5f3a\u5927\uff0c\u652f\u6301\u8de8\u5e73\u53f0\u5f00\u53d1\uff0c\u9002\u5408\u590d\u6742\u7684\u5e94\u7528\u3002<\/li>\n<li><strong>Kivy\uff1a<\/strong> \u4e13\u6ce8\u4e8e\u591a\u70b9\u89e6\u63a7\u5e94\u7528\uff0c\u9002\u5408\u79fb\u52a8\u8bbe\u5907\u3002<\/li>\n<\/ul>\n<p><h4>1. Tkinter<\/h4>\n<\/p>\n<p><p>Tkinter\u662fPython\u5185\u7f6e\u7684GUI\u5e93\uff0c\u9002\u5408\u7b80\u5355\u7684\u754c\u9762\u5e94\u7528\u3002\u5b83\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\uff0c\u4f7f\u7528\u8d77\u6765\u6bd4\u8f83\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b89\u88c5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def say_hello():<\/p>\n<p>    print(&quot;Hello, World!&quot;)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;My Tkinter App&quot;)<\/p>\n<p>button = tk.Button(root, text=&quot;Click Me&quot;, command=say_hello)<\/p>\n<p>button.pack()<\/p>\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>\u4e8c\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762<\/h3>\n<\/p>\n<p><p>\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u662f\u5c06\u529f\u80fd\u548c\u7528\u6237\u4ea4\u4e92\u7ed3\u5408\u7684\u8fc7\u7a0b\uff0c\u9700\u8981\u6ce8\u610f\u5e03\u5c40\u3001\u63a7\u4ef6\u9009\u62e9\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h4>1. \u5e03\u5c40\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u5728Tkinter\u4e2d\uff0c\u5e38\u7528\u7684\u5e03\u5c40\u7ba1\u7406\u5668\u6709pack\u3001grid\u548cplace\u3002\u9009\u62e9\u5408\u9002\u7684\u5e03\u5c40\u7ba1\u7406\u5668\u53ef\u4ee5\u8ba9\u754c\u9762\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">label = tk.Label(root, text=&quot;Enter your name:&quot;)<\/p>\n<p>label.pack()<\/p>\n<p>entry = tk.Entry(root)<\/p>\n<p>entry.pack()<\/p>\n<p>button = tk.Button(root, text=&quot;Submit&quot;, command=print_name)<\/p>\n<p>button.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u63a7\u4ef6\u9009\u62e9<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u5e94\u7528\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u63a7\u4ef6\uff0c\u5982\u6309\u94ae\u3001\u6807\u7b7e\u3001\u6587\u672c\u6846\u7b49\u3002\u6bcf\u4e2a\u63a7\u4ef6\u90fd\u6709\u81ea\u5df1\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">label = tk.Label(root, text=&quot;Hello, Tkinter!&quot;)<\/p>\n<p>label.pack()<\/p>\n<p>button = tk.Button(root, text=&quot;Exit&quot;, command=root.quit)<\/p>\n<p>button.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199\u4e8b\u4ef6\u5904\u7406\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u662f\u7528\u6237\u4e0e\u754c\u9762\u4ea4\u4e92\u65f6\u89e6\u53d1\u7684\u51fd\u6570\u3002\u5e38\u89c1\u7684\u4e8b\u4ef6\u5305\u62ec\u6309\u94ae\u70b9\u51fb\u3001\u6587\u672c\u6846\u8f93\u5165\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_name():<\/p>\n<p>    name = entry.get()<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed1\u5b9a\u4e8b\u4ef6\u5e76\u8fdb\u884c\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u5c06\u63a7\u4ef6\u4e0e\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u7ed1\u5b9a\uff0c\u901a\u8fc7\u6d4b\u8bd5\u786e\u4fdd\u529f\u80fd\u6b63\u786e\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">button = tk.Button(root, text=&quot;Submit&quot;, command=print_name)<\/p>\n<p>button.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5176\u4ed6GUI\u6846\u67b6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Tkinter\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6GUI\u6846\u67b6\u5982PyQt\u548cKivy\u3002\u5b83\u4eec\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u5408\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. PyQt<\/h4>\n<\/p>\n<p><p>PyQt\u662f\u529f\u80fd\u5f3a\u5927\u7684GUI\u5e93\uff0c\u652f\u6301\u590d\u6742\u5e94\u7528\u5f00\u53d1\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b89\u88c5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyQt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout<\/p>\n<p>def say_hello():<\/p>\n<p>    print(&quot;Hello, PyQt!&quot;)<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = QWidget()<\/p>\n<p>window.setWindowTitle(&#39;My PyQt App&#39;)<\/p>\n<p>layout = QVBoxLayout()<\/p>\n<p>button = QPushButton(&#39;Click Me&#39;)<\/p>\n<p>button.clicked.connect(say_hello)<\/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><h4>2. Kivy<\/h4>\n<\/p>\n<p><p>Kivy\u9002\u7528\u4e8e\u5f00\u53d1\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u548c\u79fb\u52a8\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b89\u88c5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install kivy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/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;Hello, Kivy!&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f18\u5316\u548c\u53d1\u5e03<\/h3>\n<\/p>\n<p><p>\u5728\u5e94\u7528\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u8fdb\u884c\u4e00\u4e9b\u4f18\u5316\uff0c\u5982\u754c\u9762\u7f8e\u5316\u3001\u529f\u80fd\u5b8c\u5584\u7b49\u3002\u6700\u540e\uff0c\u5c06\u5e94\u7528\u6253\u5305\u53d1\u5e03\u3002<\/p>\n<\/p>\n<p><p><strong>\u6253\u5305\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528<code>pyinstaller<\/code>\u5c06Python\u811a\u672c\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p>pyinstaller --onefile my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5c06Python\u811a\u672c\u505a\u6210\u754c\u9762\u5316\u662f\u4e00\u9879\u6709\u8da3\u4e14\u5b9e\u7528\u7684\u6280\u80fd\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684GUI\u6846\u67b6\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3001\u7f16\u5199\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3001\u7ed1\u5b9a\u4e8b\u4ef6\u5e76\u8fdb\u884c\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3a\u7528\u6237\u53cb\u597d\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u65e0\u8bba\u662f\u4f7f\u7528Tkinter\u3001PyQt\u8fd8\u662fKivy\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u5f00\u53d1\u7ecf\u9a8c\u9009\u62e9\u6700\u5408\u9002\u7684\u6846\u67b6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u811a\u672c\u8f6c\u5316\u4e3a\u53ef\u89c6\u5316\u754c\u9762\uff1f<\/strong><br \/>\u8981\u5c06Python\u811a\u672c\u8f6c\u5316\u4e3a\u53ef\u89c6\u5316\u754c\u9762\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6d41\u884c\u7684GUI\u6846\u67b6\uff0c\u5982Tkinter\u3001PyQt\u6216Kivy\u3002Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u9002\u5408\u521d\u5b66\u8005\uff1bPyQt\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u5f00\u53d1\u590d\u6742\u7684\u5e94\u7528\uff1bKivy\u5219\u9002\u5408\u89e6\u6478\u754c\u9762\u7684\u5e94\u7528\u3002\u9009\u62e9\u9002\u5408\u60a8\u9700\u6c42\u7684\u5e93\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u7a97\u53e3\u3001\u6309\u94ae\u3001\u6587\u672c\u6846\u7b49\u7ec4\u4ef6\uff0c\u5c06\u60a8\u7684\u811a\u672c\u529f\u80fd\u4e0e\u7528\u6237\u754c\u9762\u8fde\u63a5\u8d77\u6765\u3002<\/p>\n<p><strong>\u6211\u9700\u8981\u54ea\u4e9b\u5de5\u5177\u6765\u521b\u5efaPython GUI\uff1f<\/strong><br \/>\u521b\u5efaPython GUI\u901a\u5e38\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\u3002\u5bf9\u4e8eTkinter\uff0c\u901a\u5e38\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\uff0c\u56e0\u4e3a\u5b83\u4e0ePython\u6807\u51c6\u5e93\u6346\u7ed1\u5728\u4e00\u8d77\u3002\u5982\u679c\u9009\u62e9PyQt\u6216Kivy\uff0c\u60a8\u9700\u8981\u901a\u8fc7pip\u5b89\u88c5\u5b83\u4eec\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u6216VSCode\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7ba1\u7406\u4ee3\u7801\u548c\u8c03\u8bd5\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u73b0\u6709\u7684Python\u811a\u672c\u8f7b\u677e\u8f6c\u6362\u4e3aGUI\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u73b0\u6709\u7684Python\u811a\u672c\u53ef\u4ee5\u76f8\u5bf9\u5bb9\u6613\u5730\u8f6c\u6362\u4e3aGUI\u3002\u60a8\u9700\u8981\u5c06\u811a\u672c\u4e2d\u7684\u547d\u4ee4\u884c\u4ea4\u4e92\u66ff\u6362\u4e3a\u56fe\u5f62\u754c\u9762\u7684\u63a7\u4ef6\u3002\u4f8b\u5982\uff0c\u5c06\u7528\u6237\u8f93\u5165\u7684\u90e8\u5206\u66ff\u6362\u4e3a\u6587\u672c\u6846\u6216\u4e0b\u62c9\u83dc\u5355\uff0c\u5e76\u5c06\u811a\u672c\u7684\u8f93\u51fa\u663e\u793a\u5728\u6807\u7b7e\u6216\u6587\u672c\u533a\u57df\u4e2d\u3002\u9002\u5f53\u7684\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u53ef\u4ee5\u4f7fGUI\u54cd\u5e94\u7528\u6237\u7684\u64cd\u4f5c\uff0c\u786e\u4fdd\u7528\u6237\u4f53\u9a8c\u6d41\u7545\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06Python\u811a\u672c\u505a\u6210\u754c\u9762\u5316\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u9009\u62e9\u5408\u9002\u7684GUI\u6846\u67b6\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3001\u7f16\u5199\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3001\u7ed1\u5b9a\u4e8b\u4ef6\u5e76\u8fdb [&hellip;]","protected":false},"author":3,"featured_media":1029324,"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\/1029316"}],"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=1029316"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1029316\/revisions"}],"predecessor-version":[{"id":1029328,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1029316\/revisions\/1029328"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1029324"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1029316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1029316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1029316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}