{"id":980300,"date":"2024-12-27T06:53:25","date_gmt":"2024-12-26T22:53:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/980300.html"},"modified":"2024-12-27T06:53:28","modified_gmt":"2024-12-26T22:53:28","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e7%95%8c%e9%9d%a2%e5%a4%a7%e5%b0%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/980300.html","title":{"rendered":"Python\u5982\u4f55\u8bbe\u7f6e\u754c\u9762\u5927\u5c0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205744\/0e631a54-5d5e-4c17-aafc-c5ec8b193365.webp\" alt=\"Python\u5982\u4f55\u8bbe\u7f6e\u754c\u9762\u5927\u5c0f\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8bbe\u7f6e\u754c\u9762\u5927\u5c0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Tkinter\u5e93\u3001PyQt\u5e93\u3001Kivy\u5e93\u7b49\u3002<\/strong>\u5176\u4e2d\uff0cTkinter\u5e93\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u975e\u5e38\u9002\u5408\u521b\u5efa\u7b80\u5355\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff1bPyQt\u5e93\u63d0\u4f9b\u4e86\u4e00\u5957\u5b8c\u6574\u7684\u8de8\u5e73\u53f0GUI\u5f00\u53d1\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\uff1bKivy\u5e93\u5219\u662f\u4e00\u4e2a\u5f00\u6e90Python\u5e93\uff0c\u4e13\u6ce8\u4e8e\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tkinter\u5e93\u6765\u8bbe\u7f6e\u754c\u9762\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528TKINTER\u8bbe\u7f6e\u754c\u9762\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f\u5728Tkinter\u4e2d\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u521d\u59cb\u5316Tkinter\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165Tkinter\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\u3002\u4f7f\u7528<code>Tk()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u521d\u59cb\u5316\u4e00\u4e2a\u7a97\u53e3\u5bf9\u8c61\u3002<\/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<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>geometry()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8bbe\u7f6e\u7a97\u53e3\u7684\u5927\u5c0f\u3002\u6b64\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\u6307\u5b9a\u7a97\u53e3\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f\u4e3a800x600<\/p>\n<p>root.geometry(&#39;800x600&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u7a97\u53e3\u7684\u5bbd\u5ea6\u4e3a800\u50cf\u7d20\uff0c\u9ad8\u5ea6\u4e3a600\u50cf\u7d20\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4fee\u6539\u8fd9\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><h4>1.3 \u542f\u52a8\u4e3b\u4e8b\u4ef6\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/code>\u65b9\u6cd5\u542f\u52a8\u4e3b\u4e8b\u4ef6\u5faa\u73af\uff0c\u4ee5\u663e\u793a\u7a97\u53e3\u5e76\u54cd\u5e94\u7528\u6237\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u542f\u52a8\u4e3b\u4e8b\u4ef6\u5faa\u73af<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PYQT\u8bbe\u7f6e\u754c\u9762\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684GUI\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u9700\u8981\u66f4\u591a\u63a7\u4ef6\u548c\u590d\u6742\u754c\u9762\u5e03\u5c40\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5b89\u88c5PyQt\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5PyQt\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyQt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u521b\u5efaPyQt\u5e94\u7528\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aPyQt\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u521d\u59cb\u5316<code>QApplication<\/code>\u5bf9\u8c61\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication, QMainWindow<\/p>\n<p>import sys<\/p>\n<p>app = QApplication(sys.argv)<\/p>\n<p>window = QMainWindow()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3 \u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>resize()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8bbe\u7f6e\u7a97\u53e3\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f\u4e3a800x600<\/p>\n<p>window.resize(800, 600)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.4 \u663e\u793a\u7a97\u53e3\u5e76\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>show()<\/code>\u65b9\u6cd5\u663e\u793a\u7a97\u53e3\uff0c\u5e76\u8c03\u7528<code>exec_()<\/code>\u65b9\u6cd5\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u663e\u793a\u7a97\u53e3<\/p>\n<p>window.show()<\/p>\n<h2><strong>\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528KIVY\u8bbe\u7f6e\u754c\u9762\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u4e13\u6ce8\u4e8e\u591a\u70b9\u89e6\u63a7\u7684GUI\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u5f00\u53d1\u8de8\u5e73\u53f0\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5Kivy\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5Kivy\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install kivy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u521b\u5efaKivy\u5e94\u7528\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aKivy\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u7ee7\u627f<code>App<\/code>\u7c7b\uff0c\u5e76\u91cd\u5199<code>build()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.widget import Widget<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        return Widget()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3 \u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728Kivy\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f\uff0c\u6216\u8005\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.config import Config<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f\u4e3a800x600<\/strong><\/h2>\n<p>Config.set(&#39;graphics&#39;, &#39;width&#39;, &#39;800&#39;)<\/p>\n<p>Config.set(&#39;graphics&#39;, &#39;height&#39;, &#39;600&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.4 \u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>run()<\/code>\u65b9\u6cd5\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><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8bbe\u7f6e\u754c\u9762\u5927\u5c0f\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002<strong>Tkinter<\/strong>\u9002\u5408\u5feb\u901f\u5f00\u53d1\u7b80\u5355\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff0c<strong>PyQt<\/strong>\u63d0\u4f9b\u5f3a\u5927\u7684\u63a7\u4ef6\u548c\u5e03\u5c40\u7ba1\u7406\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\uff0c\u800c<strong>Kivy<\/strong>\u5219\u4e13\u6ce8\u4e8e\u591a\u70b9\u89e6\u63a7\u548c\u8de8\u5e73\u53f0\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u4f7f\u5f97\u5f00\u53d1\u8fc7\u7a0b\u66f4\u52a0\u9ad8\u6548\u548c\u4fbf\u6377\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u53ef\u8c03\u8282\u5927\u5c0f\u7684\u7a97\u53e3\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7a97\u53e3\u7684\u6700\u5c0f\u548c\u6700\u5927\u5c3a\u5bf8\u6765\u5b9e\u73b0\u53ef\u8c03\u8282\u7684\u7a97\u53e3\u5927\u5c0f\u3002\u4f7f\u7528tkinter\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528<code>minsize()<\/code>\u548c<code>maxsize()<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u7a97\u53e3\u7684\u6700\u5c0f\u548c\u6700\u5927\u5c3a\u5bf8\u3002\u4f8b\u5982\uff0c<code>root.minsize(width, height)<\/code>\u53ef\u4ee5\u5c06\u7a97\u53e3\u7684\u6700\u5c0f\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528tkinter\u8bbe\u7f6e\u7a97\u53e3\u7684\u521d\u59cb\u5927\u5c0f\uff1f<\/strong><br \/>\u5728tkinter\u4e2d\uff0c\u53ef\u4ee5\u5728\u521b\u5efa\u7a97\u53e3\u65f6\u901a\u8fc7<code>geometry()<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u7a97\u53e3\u7684\u521d\u59cb\u5927\u5c0f\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\u683c\u5f0f\u4e3a\u201c\u5bbd\u5ea6x\u9ad8\u5ea6\u201d\uff0c\u4f8b\u5982\uff0c<code>root.geometry(&quot;800x600&quot;)<\/code>\u5c06\u7a97\u53e3\u7684\u521d\u59cb\u5927\u5c0f\u8bbe\u7f6e\u4e3a800\u50cf\u7d20\u5bbd\u548c600\u50cf\u7d20\u9ad8\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u52a8\u6001\u8c03\u6574\u7a97\u53e3\u5927\u5c0f\uff1f<\/strong><br \/>\u8981\u5b9e\u73b0\u52a8\u6001\u8c03\u6574\u7a97\u53e3\u5927\u5c0f\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528tkinter\u4e2d\u7684\u4e8b\u4ef6\u7ed1\u5b9a\u673a\u5236\u3002\u901a\u8fc7\u7ed1\u5b9a\u7a97\u53e3\u7684\u5927\u5c0f\u53d8\u5316\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u5728\u7a97\u53e3\u5927\u5c0f\u53d8\u5316\u65f6\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\uff0c\u4f8b\u5982\u91cd\u65b0\u5e03\u5c40\u7a97\u53e3\u4e2d\u7684\u7ec4\u4ef6\u6216\u8005\u8c03\u6574\u5176\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>root.bind(&quot;&lt;Configure&gt;&quot;, resize_handler)<\/code>\u6765\u7ed1\u5b9a\u4e00\u4e2a\u5904\u7406\u51fd\u6570\uff0c\u5f53\u7a97\u53e3\u5927\u5c0f\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u8be5\u51fd\u6570\u5c06\u88ab\u8c03\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u7f6e\u754c\u9762\u5927\u5c0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Tkinter\u5e93\u3001PyQt\u5e93\u3001Kivy [&hellip;]","protected":false},"author":3,"featured_media":980311,"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\/980300"}],"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=980300"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980300\/revisions"}],"predecessor-version":[{"id":980313,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980300\/revisions\/980313"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/980311"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=980300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=980300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=980300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}