{"id":177078,"date":"2024-05-08T19:23:20","date_gmt":"2024-05-08T11:23:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/177078.html"},"modified":"2024-05-08T19:23:26","modified_gmt":"2024-05-08T11:23:26","slug":"python%e4%b8%ad%e6%80%8e%e4%b9%88%e5%9c%a8%e5%90%8c%e4%b8%80%e4%b8%aagui%e7%aa%97%e5%8f%a3%e8%ae%be%e7%bd%ae%e4%b8%a4%e4%b8%aa%e4%b8%8b%e6%8b%89%e5%88%97%e8%a1%a8%e5%91%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/177078.html","title":{"rendered":"python\u4e2d\u600e\u4e48\u5728\u540c\u4e00\u4e2agui\u7a97\u53e3\u8bbe\u7f6e\u4e24\u4e2a\u4e0b\u62c9\u5217\u8868\u5440"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060918\/f666f6fb-9cb4-4758-ad68-8acf823812f2.webp\" alt=\"python\u4e2d\u600e\u4e48\u5728\u540c\u4e00\u4e2agui\u7a97\u53e3\u8bbe\u7f6e\u4e24\u4e2a\u4e0b\u62c9\u5217\u8868\u5440\" \/><\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8981\u5728\u540c\u4e00\u4e2aGUI\u7a97\u53e3\u8bbe\u7f6e\u4e24\u4e2a\u4e0b\u62c9\u5217\u8868\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e93\uff0c<strong>\u5982Tkinter\u3001PyQt\u6216wxPython<\/strong>\u3002\u5176\u4e2d\uff0c<strong>Tkinter<\/strong>\u662f\u6700\u5e38\u7528\u7684Python GUI\u5e93\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u5185\u7f6e\u4e8e\u6807\u51c6Python\u5e93\u4e2d\u3002\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u8fc7\u7a0b\u5305\u62ec\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\uff0c\u7136\u540e\u5728\u8be5\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e24\u4e2a\u4e0b\u62c9\u5217\u8868\uff08\u4e5f\u79f0\u4e3a\u7ec4\u5408\u6846\uff09\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Tkinter\u7684<code>Tk()<\/code>\u65b9\u6cd5\u751f\u6210\u4e00\u4e2a\u7a97\u53e3\u5b9e\u4f8b\uff0c\u7136\u540e\u4f7f\u7528<code>ttk.Combobox<\/code>\u521b\u5efa\u4e0b\u62c9\u5217\u8868\u5e76\u901a\u8fc7\u914d\u7f6e\u5176<code>values<\/code>\u5c5e\u6027\u6765\u8bbe\u7f6e\u4e0b\u62c9\u9009\u9879\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5229\u7528Tkinter\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<strong>Tkinter<\/strong>\u63d0\u4f9b\u4e86<code>Tk<\/code>\u7c7b\u6765\u521b\u5efaGUI\u5e94\u7528\u7a0b\u5e8f\u7684\u4e3b\u7a97\u53e3\u3002\u5728\u521b\u5efa\u4e3b\u7a97\u53e3\u4e4b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ttk.Combobox<\/code>\u65b9\u6cd5\u521b\u5efa\u4e0b\u62c9\u5217\u8868\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5141\u8bb8\u5f00\u53d1\u8005\u5b9a\u5236\u4e0b\u62c9\u9009\u9879\u3001\u9ed8\u8ba4\u503c\u4ee5\u53ca\u5728\u7528\u6237\u9009\u62e9\u65b0\u9009\u9879\u65f6\u6fc0\u53d1\u7684\u4e8b\u4ef6\u3002\u6700\u91cd\u8981\u7684\u662f\uff0c\u60a8\u8fd8\u9700\u8981\u8c03\u7528<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/code>\u65b9\u6cd5\u6765\u542f\u52a8GUI\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u4ece\u800c\u4f7f\u7a97\u53e3\u4fdd\u6301\u663e\u793a\u72b6\u6001\u5e76\u5bf9\u7528\u6237\u64cd\u4f5c\u8fdb\u884c\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165Tkinter<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u7801\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86Tkinter\u3002\u5927\u591a\u6570Python\u5b89\u88c5\u5df2\u7ecf\u5305\u542b\u4e86Tkinter\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u5bfc\u5165\u8bed\u53e5\u6765\u6d4b\u8bd5Tkinter\u662f\u5426\u53ef\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import ttk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u51fa\u73b0\u9519\u8bef\uff0c\u8868\u793aTkinter\u5df2\u6b63\u786e\u5b89\u88c5\u5e76\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u4e3b\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u60a8\u9700\u8981\u521b\u5efaGUI\u5e94\u7528\u7a0b\u5e8f\u7684\u4e3b\u7a97\u53e3\u3002\u8fd9\u662f\u6240\u6709\u7ec4\u4ef6\uff0c\u5305\u62ec\u4e0b\u62c9\u5217\u8868\u7684\u5bb9\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e3b\u7a97\u53e3<\/p>\n<p>root = tk.Tk()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u6807\u9898<\/strong><\/h2>\n<p>root.title(&quot;Python GUI \u4e0b\u62c9\u5217\u8868\u793a\u4f8b&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>root.geometry(&quot;400x200&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6dfb\u52a0\u7b2c\u4e00\u4e2a\u4e0b\u62c9\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u6709\u4e86\u4e3b\u7a97\u53e3\u4e4b\u540e\uff0c\u60a8\u53ef\u4ee5\u6dfb\u52a0\u7b2c\u4e00\u4e2a\u4e0b\u62c9\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0b\u62c9\u5217\u8868\u7684\u9009\u9879<\/p>\n<p>options1 = [&quot;\u9009\u9879 A&quot;, &quot;\u9009\u9879 B&quot;, &quot;\u9009\u9879 C&quot;]<\/p>\n<h2><strong>\u521b\u5efa\u4e0b\u62c9\u5217\u8868<\/strong><\/h2>\n<p>combobox1 = ttk.Combobox(root, values=options1)<\/p>\n<h2><strong>\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c\u5373\u9ed8\u8ba4\u663e\u793a\u7684\u9009\u9879<\/strong><\/h2>\n<p>combobox1.current(0)<\/p>\n<h2><strong>\u653e\u7f6e\u4e0b\u62c9\u5217\u8868<\/strong><\/h2>\n<p>combobox1.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u9009\u9879\u7684\u4e0b\u62c9\u5217\u8868\uff0c\u5e76\u5c06\u5176\u9ed8\u8ba4\u503c\u8bbe\u7f6e\u4e3a\u7b2c\u4e00\u4e2a\u9009\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6dfb\u52a0\u7b2c\u4e8c\u4e2a\u4e0b\u62c9\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u6309\u7167\u6dfb\u52a0\u7b2c\u4e00\u4e2a\u4e0b\u62c9\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u7ee7\u7eed\u6dfb\u52a0\u7b2c\u4e8c\u4e2a\u4e0b\u62c9\u5217\u8868\uff0c\u53ef\u80fd\u5305\u542b\u4e0d\u540c\u7684\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b2c\u4e8c\u4e2a\u4e0b\u62c9\u5217\u8868\u7684\u9009\u9879<\/p>\n<p>options2 = [&quot;\u9009\u9879 1&quot;, &quot;\u9009\u9879 2&quot;, &quot;\u9009\u9879 3&quot;]<\/p>\n<h2><strong>\u521b\u5efa\u4e0b\u62c9\u5217\u8868<\/strong><\/h2>\n<p>combobox2 = ttk.Combobox(root, values=options2)<\/p>\n<h2><strong>\u8bbe\u7f6e\u9ed8\u8ba4\u503c<\/strong><\/h2>\n<p>combobox2.current(0)<\/p>\n<h2><strong>\u653e\u7f6e\u4e0b\u62c9\u5217\u8868<\/strong><\/h2>\n<p>combobox2.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4e8b\u4ef6\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u5e0c\u671b\u5728\u7528\u6237\u9009\u62e9\u4e0b\u62c9\u5217\u8868\u4e2d\u7684\u65b0\u9009\u9879\u65f6\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ed1\u5b9a\u4e8b\u4ef6\u8fbe\u5230\u76ee\u7684\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def on_select(event):<\/p>\n<p>    # \u83b7\u53d6\u9009\u62e9\u7684\u9009\u9879<\/p>\n<p>    print(event.widget.get())<\/p>\n<h2><strong>\u4e3a\u4e24\u4e2a\u4e0b\u62c9\u5217\u8868\u7ed1\u5b9a\u540c\u4e00\u4e2a\u4e8b\u4ef6<\/strong><\/h2>\n<p>combobox1.bind(&quot;&lt;&lt;ComboboxSelected&gt;&gt;&quot;, on_select)<\/p>\n<p>combobox2.bind(&quot;&lt;&lt;ComboboxSelected&gt;&gt;&quot;, on_select)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u542f\u52a8GUI<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528<code>mainloop()<\/code>\u65b9\u6cd5\uff0c\u542f\u52a8Tkinter\u7684\u4e3b\u4e8b\u4ef6\u5faa\u73af\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u6b65\u9aa4\u5c55\u793a\u4e86\u5728Python\u4e2d\u4f7f\u7528Tkinter\u5728\u540c\u4e00\u4e2aGUI\u7a97\u53e3\u8bbe\u7f6e\u4e24\u4e2a\u4e0b\u62c9\u5217\u8868\u7684\u5b8c\u6574\u8fc7\u7a0b\u3002\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u548c\u6dfb\u52a0\u66f4\u591a\u7684\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u66f4\u52a0\u590d\u6742\u548c\u5f3a\u5927\u7684GUI\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\u8bbe\u7f6e\u4e00\u4e2agui\u7a97\u53e3\u5e76\u540c\u65f6\u6dfb\u52a0\u4e24\u4e2a\u4e0b\u62c9\u5217\u8868\uff1f<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python Tkinter\u5e93\u521b\u5efa\u4e00\u4e2agui\u7a97\u53e3\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efaGUI\u7a97\u53e3\u3002\u6211\u4eec\u9700\u8981\u5bfc\u5165Tkinter\u5e93\u5e76\u4f7f\u7528Tk()\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\u3002<\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\nroot = tk.Tk()\nroot.mainloop()\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u5411GUI\u7a97\u53e3\u6dfb\u52a0\u4e00\u4e2a\u4e0b\u62c9\u5217\u8868\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528Tkinter\u5e93\u7684OptionMenu\u90e8\u4ef6\u53ef\u4ee5\u8f7b\u677e\u6dfb\u52a0\u4e00\u4e2a\u4e0b\u62c9\u5217\u8868\u3002\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u6765\u5b58\u50a8\u9009\u5b9a\u7684\u503c\uff0c\u5e76\u5c06\u5176\u4e0eOptionMenu\u90e8\u4ef6\u76f8\u5173\u8054\u3002<\/p>\n<pre><code class=\"language-python\">variable = tk.StringVar(root)\nvariable.set(&quot;\u9009\u62e9\u9009\u9879&quot;)\n\ndropdown = tk.OptionMenu(root, variable, &quot;\u9009\u98791&quot;, &quot;\u9009\u98792&quot;, &quot;\u9009\u98793&quot;)\ndropdown.pack()\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u5728\u540c\u4e00\u4e2aGUI\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e24\u4e2a\u4e0b\u62c9\u5217\u8868\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\u6765\u6dfb\u52a0\u7b2c\u4e8c\u4e2a\u4e0b\u62c9\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u63d0\u4f9b\u7684\u5176\u4ed6\u5e03\u5c40\u7ba1\u7406\u5668\u6765\u63a7\u5236\u4e0b\u62c9\u5217\u8868\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002<\/p>\n<pre><code class=\"language-python\">variable1 = tk.StringVar(root)\nvariable1.set(&quot;\u9009\u62e9\u9009\u98791&quot;)\n\ndropdown1 = tk.OptionMenu(root, variable1, &quot;\u9009\u98791&quot;, &quot;\u9009\u98792&quot;, &quot;\u9009\u98793&quot;)\ndropdown1.pack()\n\nvariable2 = tk.StringVar(root)\nvariable2.set(&quot;\u9009\u62e9\u9009\u98792&quot;)\n\ndropdown2 = tk.OptionMenu(root, variable2, &quot;\u9009\u98794&quot;, &quot;\u9009\u98795&quot;, &quot;\u9009\u98796&quot;)\ndropdown2.pack()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u591a\u6b21\u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u5728\u540c\u4e00\u4e2aGUI\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4efb\u610f\u6570\u91cf\u7684\u4e0b\u62c9\u5217\u8868\uff0c\u4ee5\u6ee1\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8981\u5728\u540c\u4e00\u4e2aGUI\u7a97\u53e3\u8bbe\u7f6e\u4e24\u4e2a\u4e0b\u62c9\u5217\u8868\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\u5e93\uff0c\u5982Tkinter\u3001PyQt\u6216 [&hellip;]","protected":false},"author":3,"featured_media":177088,"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\/177078"}],"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=177078"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/177078\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/177088"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=177078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=177078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=177078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}