{"id":1184974,"date":"2025-01-15T19:33:58","date_gmt":"2025-01-15T11:33:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1184974.html"},"modified":"2025-01-15T19:34:19","modified_gmt":"2025-01-15T11:34:19","slug":"python%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e7%95%8c%e9%9d%a2%e8%ae%be%e8%ae%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1184974.html","title":{"rendered":"python\u5982\u4f55\u663e\u793a\u754c\u9762\u8bbe\u8ba1"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25134238\/42ca477d-e5c7-4b00-aea9-bf14eb54b920.webp\" alt=\"python\u5982\u4f55\u663e\u793a\u754c\u9762\u8bbe\u8ba1\" \/><\/p>\n<p><p> <strong>Python\u663e\u793a\u754c\u9762\u8bbe\u8ba1\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Tkinter\u3001PyQt\u3001Kivy\u7b49\u5e93\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tkinter\u5e93\u8fdb\u884c\u754c\u9762\u8bbe\u8ba1\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Tkinter\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u7a97\u53e3\u548c\u5904\u7406\u7528\u6237\u8f93\u5165\u3002Tkinter\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u5b83\u968fPython\u5b89\u88c5\u5305\u4e00\u8d77\u63d0\u4f9b\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u57fa\u672c\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Tkinter\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7a97\u53e3\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\u9700\u8981\u5bfc\u5165Tkinter\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\u5bf9\u8c61\uff0c\u5e76\u8fdb\u5165\u4e3b\u5faa\u73af\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/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;\u6211\u7684\u7b2c\u4e00\u4e2aTkinter\u7a97\u53e3&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u8fdb\u5165\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><h3>\u4e09\u3001\u6dfb\u52a0\u63a7\u4ef6<\/h3>\n<\/p>\n<p><p>Tkinter\u63d0\u4f9b\u4e86\u591a\u79cd\u63a7\u4ef6\uff0c\u5982\u6309\u94ae\u3001\u6807\u7b7e\u3001\u6587\u672c\u6846\u7b49\uff0c\u53ef\u4ee5\u7528\u6765\u6784\u5efa\u4e30\u5bcc\u7684\u7528\u6237\u754c\u9762\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6309\u94ae\u548c\u4e00\u4e2a\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_button_click():<\/p>\n<p>    label.config(text=&quot;\u6309\u94ae\u88ab\u70b9\u51fb\u4e86&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter\u63a7\u4ef6\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6807\u7b7e<\/strong><\/h2>\n<p>label = tk.Label(root, text=&quot;\u8fd9\u662f\u4e00\u4e2a\u6807\u7b7e&quot;)<\/p>\n<p>label.pack(pady=10)<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>button = tk.Button(root, text=&quot;\u70b9\u51fb\u6211&quot;, command=on_button_click)<\/p>\n<p>button.pack(pady=10)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5e03\u5c40\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>Tkinter\u63d0\u4f9b\u4e86\u4e09\u79cd\u5e03\u5c40\u7ba1\u7406\u5668\uff1apack\u3001grid\u548cplace\u3002pack()\u65b9\u6cd5\u7528\u4e8e\u5c06\u63a7\u4ef6\u6253\u5305\u5230\u7a97\u53e3\u4e2d\uff0cgrid()\u65b9\u6cd5\u7528\u4e8e\u5c06\u63a7\u4ef6\u653e\u7f6e\u5728\u7f51\u683c\u4e2d\uff0cplace()\u65b9\u6cd5\u7528\u4e8e\u7cbe\u786e\u63a7\u5236\u63a7\u4ef6\u7684\u4f4d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528grid()\u65b9\u6cd5\u7684\u793a\u4f8b\uff1a<\/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>root.title(&quot;Tkinter\u5e03\u5c40\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6807\u7b7e\u548c\u6309\u94ae<\/strong><\/h2>\n<p>label1 = tk.Label(root, text=&quot;\u6807\u7b7e1&quot;)<\/p>\n<p>label2 = tk.Label(root, text=&quot;\u6807\u7b7e2&quot;)<\/p>\n<p>button1 = tk.Button(root, text=&quot;\u6309\u94ae1&quot;)<\/p>\n<p>button2 = tk.Button(root, text=&quot;\u6309\u94ae2&quot;)<\/p>\n<h2><strong>\u4f7f\u7528grid\u5e03\u5c40<\/strong><\/h2>\n<p>label1.grid(row=0, column=0, padx=10, pady=10)<\/p>\n<p>label2.grid(row=0, column=1, padx=10, pady=10)<\/p>\n<p>button1.grid(row=1, column=0, padx=10, pady=10)<\/p>\n<p>button2.grid(row=1, column=1, padx=10, pady=10)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4e8b\u4ef6\u5904\u7406<\/h3>\n<\/p>\n<p><p>Tkinter\u4e2d\u7684\u6bcf\u4e2a\u63a7\u4ef6\u90fd\u53ef\u4ee5\u7ed1\u5b9a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u53ef\u4ee5\u901a\u8fc7command\u53c2\u6570\u7ed1\u5b9a\u5230\u4e00\u4e2a\u51fd\u6570\u3002\u9664\u4e86\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u5176\u4ed6\u4e8b\u4ef6\uff0c\u5982\u9f20\u6807\u70b9\u51fb\u3001\u952e\u76d8\u8f93\u5165\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u952e\u76d8\u8f93\u5165\u4e8b\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_key_press(event):<\/p>\n<p>    label.config(text=f&quot;\u4f60\u6309\u4e0b\u4e86: {event.char}&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter\u4e8b\u4ef6\u5904\u7406\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6807\u7b7e<\/strong><\/h2>\n<p>label = tk.Label(root, text=&quot;\u8bf7\u6309\u4e0b\u4efb\u610f\u952e&quot;)<\/p>\n<p>label.pack(pady=10)<\/p>\n<h2><strong>\u7ed1\u5b9a\u952e\u76d8\u8f93\u5165\u4e8b\u4ef6<\/strong><\/h2>\n<p>root.bind(&quot;&lt;KeyPress&gt;&quot;, on_key_press)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u83dc\u5355\u680f\u8bbe\u8ba1<\/h3>\n<\/p>\n<p><p>Tkinter\u8fd8\u63d0\u4f9b\u4e86\u521b\u5efa\u83dc\u5355\u680f\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u5728\u4e3b\u7a97\u53e3\u4e2d\u6dfb\u52a0\u83dc\u5355\u680f\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u83dc\u5355\u9879\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_menu_click():<\/p>\n<p>    label.config(text=&quot;\u83dc\u5355\u9879\u88ab\u70b9\u51fb\u4e86&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter\u83dc\u5355\u680f\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6807\u7b7e<\/strong><\/h2>\n<p>label = tk.Label(root, text=&quot;\u8fd9\u662f\u4e00\u4e2a\u6807\u7b7e&quot;)<\/p>\n<p>label.pack(pady=10)<\/p>\n<h2><strong>\u521b\u5efa\u83dc\u5355\u680f<\/strong><\/h2>\n<p>menu_bar = tk.Menu(root)<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6\u83dc\u5355<\/strong><\/h2>\n<p>file_menu = tk.Menu(menu_bar, tearoff=0)<\/p>\n<p>file_menu.add_command(text=&quot;\u6253\u5f00&quot;, command=on_menu_click)<\/p>\n<p>file_menu.add_command(text=&quot;\u4fdd\u5b58&quot;, command=on_menu_click)<\/p>\n<p>file_menu.add_separator()<\/p>\n<p>file_menu.add_command(text=&quot;\u9000\u51fa&quot;, command=root.quit)<\/p>\n<p>menu_bar.add_cascade(label=&quot;\u6587\u4ef6&quot;, menu=file_menu)<\/p>\n<h2><strong>\u6dfb\u52a0\u83dc\u5355\u680f\u5230\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root.config(menu=menu_bar)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5bf9\u8bdd\u6846\u548c\u6d88\u606f\u6846<\/h3>\n<\/p>\n<p><p>Tkinter\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u5bf9\u8bdd\u6846\u548c\u6d88\u606f\u6846\uff0c\u53ef\u4ee5\u7528\u6765\u663e\u793a\u4fe1\u606f\u3001\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u6d88\u606f\u6846\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import messagebox<\/p>\n<p>def on_button_click():<\/p>\n<p>    messagebox.showinfo(&quot;\u4fe1\u606f&quot;, &quot;\u8fd9\u662f\u4e00\u4e2a\u6d88\u606f\u6846&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter\u6d88\u606f\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>button = tk.Button(root, text=&quot;\u663e\u793a\u6d88\u606f\u6846&quot;, command=on_button_click)<\/p>\n<p>button.pack(pady=10)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528Canvas\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p>Tkinter\u7684Canvas\u63a7\u4ef6\u5141\u8bb8\u5728\u7a97\u53e3\u4e2d\u7ed8\u5236\u56fe\u5f62\uff0c\u5982\u7ebf\u6761\u3001\u77e9\u5f62\u3001\u5706\u5f62\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Canvas\u7ed8\u5236\u7b80\u5355\u56fe\u5f62\u7684\u793a\u4f8b\uff1a<\/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>root.title(&quot;Tkinter Canvas\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u521b\u5efaCanvas<\/strong><\/h2>\n<p>canvas = tk.Canvas(root, width=400, height=300)<\/p>\n<p>canvas.pack()<\/p>\n<h2><strong>\u7ed8\u5236\u7ebf\u6761<\/strong><\/h2>\n<p>canvas.create_line(50, 50, 200, 200, fill=&quot;blue&quot;, width=2)<\/p>\n<h2><strong>\u7ed8\u5236\u77e9\u5f62<\/strong><\/h2>\n<p>canvas.create_rectangle(100, 100, 250, 200, outline=&quot;red&quot;, width=2)<\/p>\n<h2><strong>\u7ed8\u5236\u5706\u5f62<\/strong><\/h2>\n<p>canvas.create_oval(150, 150, 300, 300, outline=&quot;green&quot;, width=2)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528Scrollbar\u63a7\u4ef6<\/h3>\n<\/p>\n<p><p>Tkinter\u7684Scrollbar\u63a7\u4ef6\u7528\u4e8e\u5728\u7a97\u53e3\u4e2d\u6dfb\u52a0\u6eda\u52a8\u6761\u3002\u53ef\u4ee5\u5c06\u6eda\u52a8\u6761\u4e0e\u5176\u4ed6\u63a7\u4ef6\uff08\u5982Listbox\u3001Text\u7b49\uff09\u7ed1\u5b9a\u5728\u4e00\u8d77\uff0c\u4ee5\u5b9e\u73b0\u6eda\u52a8\u6548\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u6eda\u52a8\u6761\u7684\u793a\u4f8b\uff1a<\/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>root.title(&quot;Tkinter Scrollbar\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efaText\u63a7\u4ef6<\/strong><\/h2>\n<p>text = tk.Text(root, wrap=&quot;none&quot;)<\/p>\n<p>text.pack(side=&quot;left&quot;, fill=&quot;both&quot;, expand=True)<\/p>\n<h2><strong>\u521b\u5efa\u5782\u76f4\u6eda\u52a8\u6761<\/strong><\/h2>\n<p>v_scrollbar = tk.Scrollbar(root, orient=&quot;vertical&quot;, command=text.yview)<\/p>\n<p>v_scrollbar.pack(side=&quot;right&quot;, fill=&quot;y&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6c34\u5e73\u6eda\u52a8\u6761<\/strong><\/h2>\n<p>h_scrollbar = tk.Scrollbar(root, orient=&quot;horizontal&quot;, command=text.xview)<\/p>\n<p>h_scrollbar.pack(side=&quot;bottom&quot;, fill=&quot;x&quot;)<\/p>\n<h2><strong>\u7ed1\u5b9a\u6eda\u52a8\u6761<\/strong><\/h2>\n<p>text.config(yscrollcommand=v_scrollbar.set, xscrollcommand=h_scrollbar.set)<\/p>\n<h2><strong>\u6dfb\u52a0\u4e00\u4e9b\u5185\u5bb9\u5230Text\u63a7\u4ef6<\/strong><\/h2>\n<p>for i in range(50):<\/p>\n<p>    text.insert(&quot;end&quot;, f&quot;\u8fd9\u662f\u7b2c{i+1}\u884c\u5185\u5bb9\\n&quot;)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528Frame\u63a7\u4ef6\u7ec4\u7ec7\u754c\u9762<\/h3>\n<\/p>\n<p><p>Frame\u63a7\u4ef6\u7528\u4e8e\u5728\u7a97\u53e3\u4e2d\u521b\u5efa\u4e00\u4e2a\u5bb9\u5668\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u63a7\u4ef6\u653e\u7f6e\u5728Frame\u4e2d\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7ec4\u7ec7\u754c\u9762\u5e03\u5c40\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Frame\u63a7\u4ef6\u7684\u793a\u4f8b\uff1a<\/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>root.title(&quot;Tkinter Frame\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efaFrame<\/strong><\/h2>\n<p>frame = tk.Frame(root, padx=10, pady=10)<\/p>\n<p>frame.pack(pady=20)<\/p>\n<h2><strong>\u5728Frame\u4e2d\u6dfb\u52a0\u63a7\u4ef6<\/strong><\/h2>\n<p>label = tk.Label(frame, text=&quot;\u8fd9\u662f\u4e00\u4e2a\u6807\u7b7e&quot;)<\/p>\n<p>label.pack(pady=5)<\/p>\n<p>button = tk.Button(frame, text=&quot;\u70b9\u51fb\u6211&quot;)<\/p>\n<p>button.pack(pady=5)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528PanedWindow\u63a7\u4ef6<\/h3>\n<\/p>\n<p><p>PanedWindow\u63a7\u4ef6\u7528\u4e8e\u5728\u7a97\u53e3\u4e2d\u521b\u5efa\u53ef\u8c03\u6574\u5927\u5c0f\u7684\u9762\u677f\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u63a7\u4ef6\u653e\u7f6e\u5728\u9762\u677f\u4e2d\uff0c\u5e76\u5141\u8bb8\u7528\u6237\u8c03\u6574\u9762\u677f\u7684\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528PanedWindow\u63a7\u4ef6\u7684\u793a\u4f8b\uff1a<\/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>root.title(&quot;Tkinter PanedWindow\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6c34\u5e73PanedWindow<\/strong><\/h2>\n<p>paned_window = tk.PanedWindow(root, orient=&quot;horizontal&quot;)<\/p>\n<p>paned_window.pack(fill=&quot;both&quot;, expand=True)<\/p>\n<h2><strong>\u5728PanedWindow\u4e2d\u6dfb\u52a0Frame<\/strong><\/h2>\n<p>left_frame = tk.Frame(paned_window, bg=&quot;lightblue&quot;, width=150, height=300)<\/p>\n<p>paned_window.add(left_frame)<\/p>\n<p>right_frame = tk.Frame(paned_window, bg=&quot;lightgreen&quot;, width=250, height=300)<\/p>\n<p>paned_window.add(right_frame)<\/p>\n<h2><strong>\u5728Frame\u4e2d\u6dfb\u52a0\u63a7\u4ef6<\/strong><\/h2>\n<p>label = tk.Label(left_frame, text=&quot;\u5de6\u4fa7\u9762\u677f&quot;, bg=&quot;lightblue&quot;)<\/p>\n<p>label.pack(pady=20)<\/p>\n<p>button = tk.Button(right_frame, text=&quot;\u53f3\u4fa7\u9762\u677f&quot;)<\/p>\n<p>button.pack(pady=20)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528Notebook\u63a7\u4ef6<\/h3>\n<\/p>\n<p><p>Notebook\u63a7\u4ef6\u7528\u4e8e\u5728\u7a97\u53e3\u4e2d\u521b\u5efa\u9009\u9879\u5361\u754c\u9762\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2aFrame\u653e\u7f6e\u5728Notebook\u4e2d\uff0c\u5e76\u901a\u8fc7\u9009\u9879\u5361\u8fdb\u884c\u5207\u6362\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Notebook\u63a7\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import ttk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter Notebook\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u521b\u5efaNotebook<\/strong><\/h2>\n<p>notebook = ttk.Notebook(root)<\/p>\n<p>notebook.pack(fill=&quot;both&quot;, expand=True)<\/p>\n<h2><strong>\u521b\u5efaFrame<\/strong><\/h2>\n<p>frame1 = tk.Frame(notebook, bg=&quot;lightblue&quot;)<\/p>\n<p>frame2 = tk.Frame(notebook, bg=&quot;lightgreen&quot;)<\/p>\n<h2><strong>\u6dfb\u52a0Frame\u5230Notebook<\/strong><\/h2>\n<p>notebook.add(frame1, text=&quot;\u9009\u9879\u53611&quot;)<\/p>\n<p>notebook.add(frame2, text=&quot;\u9009\u9879\u53612&quot;)<\/p>\n<h2><strong>\u5728Frame\u4e2d\u6dfb\u52a0\u63a7\u4ef6<\/strong><\/h2>\n<p>label1 = tk.Label(frame1, text=&quot;\u8fd9\u662f\u9009\u9879\u53611&quot;, bg=&quot;lightblue&quot;)<\/p>\n<p>label1.pack(pady=20)<\/p>\n<p>label2 = tk.Label(frame2, text=&quot;\u8fd9\u662f\u9009\u9879\u53612&quot;, bg=&quot;lightgreen&quot;)<\/p>\n<p>label2.pack(pady=20)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528Treeview\u63a7\u4ef6<\/h3>\n<\/p>\n<p><p>Treeview\u63a7\u4ef6\u7528\u4e8e\u5728\u7a97\u53e3\u4e2d\u521b\u5efa\u6811\u5f62\u89c6\u56fe\uff0c\u53ef\u4ee5\u663e\u793a\u5206\u5c42\u7ed3\u6784\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Treeview\u63a7\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import ttk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter Treeview\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u521b\u5efaTreeview<\/strong><\/h2>\n<p>tree = ttk.Treeview(root)<\/p>\n<p>tree.pack(fill=&quot;both&quot;, expand=True)<\/p>\n<h2><strong>\u6dfb\u52a0\u6839\u8282\u70b9<\/strong><\/h2>\n<p>root_node = tree.insert(&quot;&quot;, &quot;end&quot;, text=&quot;\u6839\u8282\u70b9&quot;)<\/p>\n<h2><strong>\u6dfb\u52a0\u5b50\u8282\u70b9<\/strong><\/h2>\n<p>tree.insert(root_node, &quot;end&quot;, text=&quot;\u5b50\u8282\u70b91&quot;)<\/p>\n<p>tree.insert(root_node, &quot;end&quot;, text=&quot;\u5b50\u8282\u70b92&quot;)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528\u8fdb\u5ea6\u6761\u63a7\u4ef6<\/h3>\n<\/p>\n<p><p>\u8fdb\u5ea6\u6761\u63a7\u4ef6\u7528\u4e8e\u5728\u7a97\u53e3\u4e2d\u663e\u793a\u4efb\u52a1\u7684\u8fdb\u5ea6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u8fdb\u5ea6\u6761\u63a7\u4ef6\u7684\u793a\u4f8b\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>def start_progress():<\/p>\n<p>    progress[&quot;value&quot;] = 0<\/p>\n<p>    root.after(100, update_progress)<\/p>\n<p>def update_progress():<\/p>\n<p>    if progress[&quot;value&quot;] &lt; 100:<\/p>\n<p>        progress[&quot;value&quot;] += 10<\/p>\n<p>        root.after(100, update_progress)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter \u8fdb\u5ea6\u6761\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u8fdb\u5ea6\u6761<\/strong><\/h2>\n<p>progress = ttk.Progressbar(root, orient=&quot;horizontal&quot;, length=200, mode=&quot;determinate&quot;)<\/p>\n<p>progress.pack(pady=20)<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>button = tk.Button(root, text=&quot;\u5f00\u59cb&quot;, command=start_progress)<\/p>\n<p>button.pack(pady=20)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u521b\u5efa\u590d\u6742\u7684GUI\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u7ec4\u5408\u4f7f\u7528Tkinter\u7684\u5404\u79cd\u63a7\u4ef6\u548c\u5e03\u5c40\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u7684GUI\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u5668\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_button_click(event):<\/p>\n<p>    text = event.widget.cget(&quot;text&quot;)<\/p>\n<p>    if text == &quot;=&quot;:<\/p>\n<p>        try:<\/p>\n<p>            result = eval(entry.get())<\/p>\n<p>            entry.delete(0, tk.END)<\/p>\n<p>            entry.insert(tk.END, str(result))<\/p>\n<p>        except Exception as e:<\/p>\n<p>            entry.delete(0, tk.END)<\/p>\n<p>            entry.insert(tk.END, &quot;\u9519\u8bef&quot;)<\/p>\n<p>    elif text == &quot;C&quot;:<\/p>\n<p>        entry.delete(0, tk.END)<\/p>\n<p>    else:<\/p>\n<p>        entry.insert(tk.END, text)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Tkinter \u8ba1\u7b97\u5668&quot;)<\/p>\n<p>root.geometry(&quot;300x400&quot;)<\/p>\n<h2><strong>\u521b\u5efaEntry\u63a7\u4ef6<\/strong><\/h2>\n<p>entry = tk.Entry(root, font=(&quot;Arial&quot;, 18), justify=&quot;right&quot;)<\/p>\n<p>entry.pack(fill=&quot;both&quot;, pady=10)<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>buttons = [<\/p>\n<p>    &quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;\/&quot;,<\/p>\n<p>    &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;*&quot;,<\/p>\n<p>    &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;-&quot;,<\/p>\n<p>    &quot;C&quot;, &quot;0&quot;, &quot;=&quot;, &quot;+&quot;<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528Frame\u7ec4\u7ec7\u6309\u94ae\u5e03\u5c40<\/strong><\/h2>\n<p>frame = tk.Frame(root)<\/p>\n<p>frame.pack()<\/p>\n<p>for i, text in enumerate(buttons):<\/p>\n<p>    button = tk.Button(frame, text=text, font=(&quot;Arial&quot;, 18), width=4, height=2)<\/p>\n<p>    button.grid(row=i\/\/4, column=i%4, padx=5, pady=5)<\/p>\n<p>    button.bind(&quot;&lt;Button-1&gt;&quot;, on_button_click)<\/p>\n<h2><strong>\u8fdb\u5165\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u4e2d\u6700\u5e38\u7528\u7684GUI\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u5e03\u5c40\u7ba1\u7406\u5668\uff0c\u80fd\u591f\u6ee1\u8db3\u5927\u591a\u6570GUI\u5e94\u7528\u7684\u9700\u6c42\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u53ef\u4ee5\u638c\u63e1\u4f7f\u7528Tkinter\u8fdb\u884c\u754c\u9762\u8bbe\u8ba1\u7684\u57fa\u672c\u65b9\u6cd5\u548c\u6280\u5de7\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u5728Python GUI\u7f16\u7a0b\u7684\u9053\u8def\u4e0a\u8fc8\u51fa\u575a\u5b9e\u7684\u4e00\u6b65\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u8fdb\u884c\u754c\u9762\u8bbe\u8ba1\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5f3a\u5927\u7684\u5e93\u7528\u4e8e\u754c\u9762\u8bbe\u8ba1\uff0c\u5176\u4e2d\u6bd4\u8f83\u6d41\u884c\u7684\u5305\u62ecTkinter\u3001PyQt\u3001Kivy\u548cwxPython\u3002Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u6613\u4e8e\u5b66\u4e60\u548c\u4f7f\u7528\u3002PyQt\u5219\u9002\u5408\u9700\u8981\u5f00\u53d1\u590d\u6742\u548c\u9ad8\u6027\u80fd\u5e94\u7528\u7a0b\u5e8f\u7684\u7528\u6237\u3002Kivy\u4e13\u6ce8\u4e8e\u591a\u70b9\u89e6\u63a7\u548c\u79fb\u52a8\u5e94\u7528\uff0c\u800cwxPython\u5219\u63d0\u4f9b\u4e86\u539f\u751f\u5916\u89c2\u548c\u611f\u89c9\u7684\u754c\u9762\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\uff1f<\/strong><br \/>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684GUI\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7Tkinter\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165Tkinter\u5e93\uff0c\u5e76\u4f7f\u7528\u57fa\u672c\u7684\u7a97\u53e3\u548c\u63a7\u4ef6\uff08\u5982\u6309\u94ae\u3001\u6807\u7b7e\u548c\u6587\u672c\u6846\uff09\u6765\u6784\u5efa\u754c\u9762\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>Tk()<\/code>\u51fd\u6570\u521b\u5efa\u4e3b\u7a97\u53e3\uff0c<code>Label()<\/code>\u548c<code>Button()<\/code>\u51fd\u6570\u6dfb\u52a0\u63a7\u4ef6\uff0c\u6700\u540e\u901a\u8fc7<code>mainloop()<\/code>\u65b9\u6cd5\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u754c\u9762\u8bbe\u8ba1\u4e2d\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u548c\u4e8b\u4ef6\uff1f<\/strong><br \/>\u5728Python\u7684GUI\u7f16\u7a0b\u4e2d\uff0c\u4e8b\u4ef6\u5904\u7406\u662f\u5173\u952e\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u7ed1\u5b9a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u6765\u54cd\u5e94\u7528\u6237\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>bind()<\/code>\u65b9\u6cd5\u5c06\u6309\u94ae\u70b9\u51fb\u4e0e\u7279\u5b9a\u529f\u80fd\u5173\u8054\u8d77\u6765\u3002\u8fd9\u6837\uff0c\u5f53\u7528\u6237\u4e0e\u754c\u9762\u4ea4\u4e92\u65f6\uff0c\u60a8\u53ef\u4ee5\u6267\u884c\u76f8\u5e94\u7684\u4ee3\u7801\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7684\u54cd\u5e94\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u663e\u793a\u754c\u9762\u8bbe\u8ba1\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Tkinter\u3001PyQt\u3001Kivy\u7b49\u5e93\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528T [&hellip;]","protected":false},"author":3,"featured_media":1184998,"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\/1184974"}],"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=1184974"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184974\/revisions"}],"predecessor-version":[{"id":1185001,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184974\/revisions\/1185001"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1184998"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1184974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1184974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1184974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}