{"id":1001168,"date":"2024-12-27T09:57:44","date_gmt":"2024-12-27T01:57:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1001168.html"},"modified":"2024-12-27T09:57:50","modified_gmt":"2024-12-27T01:57:50","slug":"python%e7%9a%84tkinter%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1001168.html","title":{"rendered":"Python\u7684tkinter\u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075440\/ba162716-c8e5-4dc6-b359-0f22f8416562.webp\" alt=\"Python\u7684tkinter\u5982\u4f55\u4f7f\u7528\" \/><\/p>\n<p><p> <strong>Python\u7684Tkinter\u5e93\u662f\u4e00\u79cd\u7528\u4e8e\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u7684\u5de5\u5177\uff0c\u5b83\u7b80\u5355\u6613\u7528\u3001\u529f\u80fd\u5f3a\u5927\u3001\u8de8\u5e73\u53f0\u517c\u5bb9\u3002\u8981\u4f7f\u7528Tkinter\uff0c\u4f60\u9700\u8981\u4e86\u89e3\u5982\u4f55\u521b\u5efa\u4e3b\u7a97\u53e3\u3001\u6dfb\u52a0\u63a7\u4ef6\uff08\u5982\u6309\u94ae\u3001\u6807\u7b7e\u3001\u8f93\u5165\u6846\u7b49\uff09\u3001\u5e03\u5c40\u7ba1\u7406\u4ee5\u53ca\u4e8b\u4ef6\u5904\u7406\u673a\u5236\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165Tkinter\u5e93\uff0c\u521b\u5efa\u4e3b\u7a97\u53e3\uff0c\u7136\u540e\u6839\u636e\u9700\u8981\u6dfb\u52a0\u4e0d\u540c\u7684\u63a7\u4ef6\uff0c\u5e76\u901a\u8fc7\u5e03\u5c40\u7ba1\u7406\u5668\u6765\u63a7\u5236\u63a7\u4ef6\u7684\u6392\u5e03\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001Tkinter\u57fa\u7840\u4ecb\u7ecd<\/p>\n<\/p>\n<p><p>Python\u7684Tkinter\u5e93\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u5b83\u57fa\u4e8eTk GUI\u5de5\u5177\u5305\u3002Tkinter\u901a\u8fc7\u63d0\u4f9b\u4e00\u7cfb\u5217\u9884\u5b9a\u4e49\u7684\u5c0f\u90e8\u4ef6\uff08\u5982\u6309\u94ae\u3001\u6587\u672c\u6846\u3001\u83dc\u5355\u7b49\uff09\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u5feb\u901f\u521b\u5efa\u56fe\u5f62\u754c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002Tkinter\u662f\u8de8\u5e73\u53f0\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u7f16\u5199\u7684GUI\u4ee3\u7801\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\uff0c\u5982Windows\u3001MacOS\u548cLinux\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Tkinter\u7684\u7b2c\u4e00\u6b65\u662f\u5bfc\u5165\u8be5\u5e93\u3002Python 3\u4e2d\uff0cTkinter\u88ab\u79f0\u4e3a<code>tkinter<\/code>\uff0c\u800c\u5728Python 2\u4e2d\uff0c\u5b83\u88ab\u79f0\u4e3a<code>Tkinter<\/code>\u3002\u5728\u5f00\u59cb\u4f7f\u7528Tkinter\u521b\u5efaGUI\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5Python\u7684Tkinter\u5e93\uff08\u4e00\u822c\u60c5\u51b5\u4e0bPython\u4f1a\u81ea\u5e26\u8be5\u5e93\uff09\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u521b\u5efa\u4e3b\u7a97\u53e3<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aTkinter\u5e94\u7528\u7a0b\u5e8f\u7684\u7b2c\u4e00\u6b65\u662f\u521d\u59cb\u5316\u4e00\u4e2a\u4e3b\u7a97\u53e3\u3002\u4e3b\u7a97\u53e3\u662f\u6240\u6709Tkinter\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u7840\uff0c\u5b83\u7528\u4e8e\u5bb9\u7eb3\u5176\u4ed6\u6240\u6709\u7684\u90e8\u4ef6\uff08widgets\uff09\u3002\u8981\u521b\u5efa\u4e3b\u7a97\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u6a21\u5757\u4e2d\u7684<code>Tk<\/code>\u7c7b\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<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u6807\u9898<\/strong><\/h2>\n<p>root.title(&quot;My Tkinter App&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5c3a\u5bf8<\/strong><\/h2>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\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>\u5728\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86Tkinter\u5e93\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>root<\/code>\u7684\u4e3b\u7a97\u53e3\uff0c\u5e76\u8bbe\u7f6e\u4e86\u7a97\u53e3\u7684\u6807\u9898\u548c\u5c3a\u5bf8\u3002\u6700\u540e\uff0c\u6211\u4eec\u8c03\u7528<code>mainloop()<\/code>\u65b9\u6cd5\u6765\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u4f7f\u7a97\u53e3\u4fdd\u6301\u663e\u793a\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6dfb\u52a0\u63a7\u4ef6\uff08Widgets\uff09<\/p>\n<\/p>\n<p><p>Tkinter\u63d0\u4f9b\u4e86\u591a\u79cd\u63a7\u4ef6\uff08widgets\uff09\u4f9b\u5f00\u53d1\u8005\u4f7f\u7528\uff0c\u5982\u6309\u94ae\u3001\u6807\u7b7e\u3001\u6587\u672c\u6846\u7b49\u3002\u4e0b\u9762\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u7528\u7684\u63a7\u4ef6\u53ca\u5176\u7528\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6807\u7b7e\uff08Label\uff09<\/strong><\/li>\n<\/ol>\n<p><p>\u6807\u7b7e\u7528\u4e8e\u5728\u754c\u9762\u4e0a\u663e\u793a\u6587\u672c\u6216\u56fe\u50cf\u3002\u53ef\u4ee5\u901a\u8fc7<code>Label<\/code>\u7c7b\u521b\u5efa\u4e00\u4e2a\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">label = tk.Label(root, text=&quot;Hello, Tkinter!&quot;)<\/p>\n<p>label.pack()  # \u4f7f\u7528pack()\u65b9\u6cd5\u5c06\u63a7\u4ef6\u6dfb\u52a0\u5230\u7a97\u53e3\u4e2d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6309\u94ae\uff08Button\uff09<\/strong><\/li>\n<\/ol>\n<p><p>\u6309\u94ae\u63a7\u4ef6\u7528\u4e8e\u6267\u884c\u67d0\u4e2a\u547d\u4ee4\u6216\u89e6\u53d1\u67d0\u4e2a\u4e8b\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7<code>Button<\/code>\u7c7b\u521b\u5efa\u4e00\u4e2a\u6309\u94ae\uff0c\u5e76\u4f7f\u7528<code>command<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\u6267\u884c\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def on_button_click():<\/p>\n<p>    print(&quot;Button clicked!&quot;)<\/p>\n<p>button = tk.Button(root, text=&quot;Click Me&quot;, command=on_button_click)<\/p>\n<p>button.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8f93\u5165\u6846\uff08Entry\uff09<\/strong><\/li>\n<\/ol>\n<p><p>\u8f93\u5165\u6846\u63a7\u4ef6\u7528\u4e8e\u63a5\u53d7\u7528\u6237\u7684\u6587\u672c\u8f93\u5165\u3002\u53ef\u4ee5\u901a\u8fc7<code>Entry<\/code>\u7c7b\u521b\u5efa\u4e00\u4e2a\u8f93\u5165\u6846\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">entry = tk.Entry(root)<\/p>\n<p>entry.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u591a\u884c\u6587\u672c\u6846\uff08Text\uff09<\/strong><\/li>\n<\/ol>\n<p><p>\u591a\u884c\u6587\u672c\u6846\u63a7\u4ef6\u5141\u8bb8\u7528\u6237\u8f93\u5165\u591a\u884c\u6587\u672c\u3002\u53ef\u4ee5\u901a\u8fc7<code>Text<\/code>\u7c7b\u521b\u5efa\u4e00\u4e2a\u591a\u884c\u6587\u672c\u6846\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = tk.Text(root, height=5, width=40)<\/p>\n<p>text.pack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5e03\u5c40\u7ba1\u7406<\/p>\n<\/p>\n<p><p>Tkinter\u63d0\u4f9b\u4e86\u4e09\u79cd\u5e03\u5c40\u7ba1\u7406\u5668\uff1a<code>pack<\/code>\u3001<code>grid<\/code>\u548c<code>place<\/code>\uff0c\u7528\u4e8e\u63a7\u5236\u63a7\u4ef6\u5728\u7a97\u53e3\u4e2d\u7684\u4f4d\u7f6e\u548c\u6392\u5217\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>pack()<\/strong><\/li>\n<\/ol>\n<p><p><code>pack()<\/code>\u65b9\u6cd5\u662fTkinter\u4e2d\u6700\u7b80\u5355\u7684\u5e03\u5c40\u7ba1\u7406\u5668\u3002\u5b83\u6839\u636e\u63a7\u4ef6\u7684\u6dfb\u52a0\u987a\u5e8f\uff0c\u4ece\u4e0a\u5230\u4e0b\u6216\u4ece\u5de6\u5230\u53f3\u6392\u5217\u63a7\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">label.pack(side=tk.TOP, fill=tk.X)  # \u5c06\u6807\u7b7e\u653e\u7f6e\u5728\u9876\u90e8\uff0c\u5e76\u6c34\u5e73\u586b\u5145<\/p>\n<p>button.pack(side=tk.BOTTOM)  # \u5c06\u6309\u94ae\u653e\u7f6e\u5728\u5e95\u90e8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>grid()<\/strong><\/li>\n<\/ol>\n<p><p><code>grid()<\/code>\u65b9\u6cd5\u5141\u8bb8\u4f60\u901a\u8fc7\u884c\u548c\u5217\u7684\u65b9\u5f0f\u6765\u6392\u5217\u63a7\u4ef6\uff0c\u66f4\u52a0\u7075\u6d3b\u3002\u4f60\u9700\u8981\u4e3a\u6bcf\u4e2a\u63a7\u4ef6\u6307\u5b9a\u5b83\u6240\u5728\u7684\u884c\u548c\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">label.grid(row=0, column=0)<\/p>\n<p>entry.grid(row=1, column=0)<\/p>\n<p>button.grid(row=2, column=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>place()<\/strong><\/li>\n<\/ol>\n<p><p><code>place()<\/code>\u65b9\u6cd5\u5141\u8bb8\u4f60\u901a\u8fc7\u6307\u5b9a\u63a7\u4ef6\u7684\u7edd\u5bf9\u4f4d\u7f6e\u6765\u6392\u5217\u63a7\u4ef6\u3002\u4f60\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u63a7\u4ef6\u6307\u5b9ax\u548cy\u5750\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">label.place(x=50, y=50)<\/p>\n<p>button.place(x=50, y=100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4e8b\u4ef6\u5904\u7406<\/p>\n<\/p>\n<p><p>\u4e8b\u4ef6\u5904\u7406\u662fGUI\u7f16\u7a0b\u7684\u91cd\u8981\u90e8\u5206\u3002Tkinter\u5141\u8bb8\u4f60\u4e3a\u63a7\u4ef6\u7ed1\u5b9a\u4e8b\u4ef6\uff0c\u4f8b\u5982\u9f20\u6807\u70b9\u51fb\u3001\u952e\u76d8\u8f93\u5165\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528<code>bind()<\/code>\u65b9\u6cd5\u4e3a\u63a7\u4ef6\u7ed1\u5b9a\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def on_key_press(event):<\/p>\n<p>    print(f&quot;Key pressed: {event.keysym}&quot;)<\/p>\n<p>root.bind(&quot;&lt;KeyPress&gt;&quot;, on_key_press)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e3a\u4e3b\u7a97\u53e3\u7ed1\u5b9a\u4e86\u4e00\u4e2a\u952e\u76d8\u6309\u4e0b\u4e8b\u4ef6\u3002\u6bcf\u5f53\u7528\u6237\u6309\u4e0b\u952e\u76d8\u4e0a\u7684\u4efb\u610f\u952e\u65f6\uff0c<code>on_key_press<\/code>\u51fd\u6570\u5c31\u4f1a\u88ab\u8c03\u7528\uff0c\u5e76\u6253\u5370\u51fa\u6309\u4e0b\u7684\u952e\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u7efc\u5408\u5b9e\u4f8b<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Tkinter\u5e94\u7528\u7a0b\u5e8f\u793a\u4f8b\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u6807\u7b7e\u3001\u4e00\u4e2a\u6309\u94ae\u548c\u4e00\u4e2a\u8f93\u5165\u6846\u3002\u5f53\u7528\u6237\u5728\u8f93\u5165\u6846\u4e2d\u8f93\u5165\u6587\u672c\u5e76\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u6807\u7b7e\u4f1a\u663e\u793a\u8f93\u5165\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def update_label():<\/p>\n<p>    text = entry.get()<\/p>\n<p>    label.config(text=text)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Simple Tkinter App&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<p>label = tk.Label(root, text=&quot;Enter text and click the button&quot;)<\/p>\n<p>label.pack(pady=10)<\/p>\n<p>entry = tk.Entry(root)<\/p>\n<p>entry.pack(pady=10)<\/p>\n<p>button = tk.Button(root, text=&quot;Update Label&quot;, command=update_label)<\/p>\n<p>button.pack(pady=10)<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5305\u542b\u4e00\u4e2a\u6807\u7b7e\u3001\u4e00\u4e2a\u8f93\u5165\u6846\u548c\u4e00\u4e2a\u6309\u94ae\u3002\u7528\u6237\u53ef\u4ee5\u5728\u8f93\u5165\u6846\u4e2d\u8f93\u5165\u6587\u672c\uff0c\u5e76\u901a\u8fc7\u70b9\u51fb\u6309\u94ae\u66f4\u65b0\u6807\u7b7e\u4e0a\u7684\u6587\u5b57\u3002\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u4e86<code>pack()<\/code>\u5e03\u5c40\u7ba1\u7406\u5668\u6765\u6392\u5217\u63a7\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Tkinter\u662fPython\u4e2d\u7528\u4e8e\u521b\u5efaGUI\u5e94\u7528\u7a0b\u5e8f\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\u3002\u901a\u8fc7\u4e86\u89e3\u5982\u4f55\u521b\u5efa\u4e3b\u7a97\u53e3\u3001\u6dfb\u52a0\u63a7\u4ef6\u3001\u4f7f\u7528\u5e03\u5c40\u7ba1\u7406\u5668\u4ee5\u53ca\u5904\u7406\u4e8b\u4ef6\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u529f\u80fd\u4e30\u5bcc\u3001\u754c\u9762\u53cb\u597d\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u867d\u7136Tkinter\u76f8\u5bf9\u7b80\u5355\uff0c\u4f46\u5b83\u8db3\u4ee5\u6ee1\u8db3\u5927\u591a\u6570\u57fa\u672c\u7684GUI\u5f00\u53d1\u9700\u6c42\uff0c\u5e76\u4e14\u7531\u4e8e\u5176\u5185\u7f6e\u4e8ePython\u4e2d\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\u3002\u901a\u8fc7\u7ec3\u4e60\u548c\u9879\u76ee\u5f00\u53d1\uff0c\u4f60\u5c06\u9010\u6b65\u638c\u63e1Tkinter\u7684\u5404\u79cd\u529f\u80fd\uff0c\u5e76\u80fd\u521b\u9020\u51fa\u66f4\u590d\u6742\u548c\u4e13\u4e1a\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u5b89\u88c5Python\u7684tkinter\u5e93\uff1f<\/strong><br \/>tkinter\u901a\u5e38\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u5728\u5927\u591a\u6570Python\u5b89\u88c5\u4e2d\u90fd\u4f1a\u9ed8\u8ba4\u5305\u542b\u5b83\u3002\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662fWindows\u6216Mac\u7cfb\u7edf\uff0ctkinter\u5e94\u81ea\u52a8\u5b89\u88c5\u3002\u5bf9\u4e8eLinux\u7528\u6237\uff0c\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5\uff0c\u4f8b\u5982\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c<code>sudo apt-get install python3-tk<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165tkinter\u6a21\u5757\u6765\u9a8c\u8bc1\u5176\u662f\u5426\u53ef\u7528\u3002<\/p>\n<p><strong>2. \u4f7f\u7528tkinter\u521b\u5efa\u7b80\u5355\u7684\u7a97\u53e3\u9700\u8981\u54ea\u4e9b\u6b65\u9aa4\uff1f<\/strong><br \/>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684tkinter\u7a97\u53e3\u9700\u8981\u51e0\u4e2a\u6b65\u9aa4\u3002\u9996\u5148\uff0c\u5bfc\u5165tkinter\u5e93\u3002\u63a5\u7740\uff0c\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\u5bf9\u8c61\uff0c\u901a\u5e38\u662f<code>Tk()<\/code>\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u7a97\u53e3\u7684\u6807\u9898\u548c\u5927\u5c0f\u3002\u6700\u540e\uff0c\u8c03\u7528<code>mainloop()<\/code>\u65b9\u6cd5\u6765\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\uff0c\u4f7f\u7a97\u53e3\u4fdd\u6301\u6253\u5f00\u72b6\u6001\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\nroot = tk.Tk()\nroot.title(&quot;\u6211\u7684\u7b2c\u4e00\u4e2a\u7a97\u53e3&quot;)\nroot.geometry(&quot;400x300&quot;)\nroot.mainloop()\n<\/code><\/pre>\n<p><strong>3. tkinter\u53ef\u4ee5\u5b9e\u73b0\u54ea\u4e9b\u5e38\u89c1\u7684GUI\u7ec4\u4ef6\uff1f<\/strong><br \/>tkinter\u63d0\u4f9b\u4e86\u591a\u79cd\u5e38\u89c1\u7684GUI\u7ec4\u4ef6\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u6309\u94ae\uff08Button\uff09\u3001\u6807\u7b7e\uff08Label\uff09\u3001\u6587\u672c\u6846\uff08Entry\uff09\u3001\u6587\u672c\u533a\u57df\uff08Text\uff09\u3001\u4e0b\u62c9\u83dc\u5355\uff08OptionMenu\uff09\u3001\u590d\u9009\u6846\uff08Checkbutton\uff09\u548c\u5355\u9009\u6309\u94ae\uff08Radiobutton\uff09\u3002\u8fd9\u4e9b\u7ec4\u4ef6\u53ef\u4ee5\u7ec4\u5408\u4f7f\u7528\uff0c\u521b\u5efa\u4e30\u5bcc\u7684\u7528\u6237\u754c\u9762\u3002\u6bcf\u79cd\u7ec4\u4ef6\u90fd\u6709\u5176\u7279\u5b9a\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u5b9a\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7684Tkinter\u5e93\u662f\u4e00\u79cd\u7528\u4e8e\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u7684\u5de5\u5177\uff0c\u5b83\u7b80\u5355\u6613\u7528\u3001\u529f\u80fd\u5f3a\u5927\u3001\u8de8\u5e73\u53f0\u517c\u5bb9\u3002\u8981 [&hellip;]","protected":false},"author":3,"featured_media":1001186,"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\/1001168"}],"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=1001168"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001168\/revisions"}],"predecessor-version":[{"id":1001188,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001168\/revisions\/1001188"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1001186"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1001168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1001168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1001168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}