{"id":961106,"date":"2024-12-27T03:55:39","date_gmt":"2024-12-26T19:55:39","guid":{"rendered":""},"modified":"2024-12-27T03:55:42","modified_gmt":"2024-12-26T19:55:42","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%balistbox","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/961106.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u521b\u5efalistbox"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103421\/c2ce777e-d6cf-4b63-9088-d379d3fb2b81.webp\" alt=\"python\u4e2d\u5982\u4f55\u521b\u5efalistbox\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u521b\u5efaListbox\u7684\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528Tkinter\u5e93\u3002<strong>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93<\/strong>\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u901f\u800c\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002\u4ee5\u4e0b\u662f\u521b\u5efaListbox\u7684\u4e00\u4e9b\u6b65\u9aa4\uff1a<strong>\u5bfc\u5165Tkinter\u6a21\u5757\u3001\u521b\u5efa\u4e3b\u7a97\u53e3\u3001\u521b\u5efaListbox\u3001\u6dfb\u52a0\u9879\u76ee\u5230Listbox\u4e2d\u3001\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u4e2a\u6b65\u9aa4\uff1a\u521b\u5efaListbox\u3002<\/p>\n<\/p>\n<p><p><strong>\u5728\u521b\u5efaListbox\u65f6\uff0c\u4f60\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u9009\u9879\u6765\u5b9a\u4e49\u5176\u884c\u4e3a\u548c\u5916\u89c2<\/strong>\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6eListbox\u7684\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u3001\u9009\u62e9\u6a21\u5f0f\uff08\u5982\u5355\u9009\u6216\u591a\u9009\uff09\u3001\u80cc\u666f\u8272\u548c\u524d\u666f\u8272\u7b49\u3002\u901a\u8fc7\u914d\u7f6e\u8fd9\u4e9b\u9009\u9879\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u7b26\u5408\u4f60\u9700\u6c42\u7684Listbox\u3002\u4e0b\u9762\u662f\u521b\u5efaListbox\u7684\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\u4ee3\u7801\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;Listbox \u793a\u4f8b&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aListbox<\/strong><\/h2>\n<p>listbox = tk.Listbox(root, selectmode=tk.SINGLE, width=40, height=10, bg=&#39;lightyellow&#39;, fg=&#39;blue&#39;)<\/p>\n<h2><strong>\u5411Listbox\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u9879\u76ee<\/strong><\/h2>\n<p>items = [&quot;Python&quot;, &quot;Java&quot;, &quot;C++&quot;, &quot;JavaScript&quot;, &quot;Go&quot;, &quot;Swift&quot;]<\/p>\n<p>for item in items:<\/p>\n<p>    listbox.insert(tk.END, item)<\/p>\n<h2><strong>\u5c06Listbox\u653e\u7f6e\u5728\u4e3b\u7a97\u53e3\u4e2d<\/strong><\/h2>\n<p>listbox.pack(pady=20)<\/p>\n<h2><strong>\u8fd0\u884c\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>\u4e00\u3001\u5bfc\u5165\u548c\u521d\u59cb\u5316Tkinter<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u521b\u5efaListbox\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165Tkinter\u5e93\u5e76\u521d\u59cb\u5316\u5e94\u7528\u7a0b\u5e8f\u7a97\u53e3\u3002\u8fd9\u662f\u521b\u5efa\u4efb\u4f55Tkinter\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u7840\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5bfc\u5165Tkinter\u5e93<\/strong>\uff1aTkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u56e0\u6b64\u4f60\u53ea\u9700\u7b80\u5355\u5730\u4f7f\u7528<code>import tkinter as tk<\/code>\u6765\u5bfc\u5165\u5b83\u3002<\/li>\n<li><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong>\uff1a\u4f7f\u7528<code>tk.Tk()<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u4e3b\u7a97\u53e3\u3002\u8fd9\u662f\u5e94\u7528\u7a0b\u5e8f\u7684\u4e3b\u754c\u9762\uff0c\u6240\u6709\u7684\u7ec4\u4ef6\u90fd\u4f1a\u653e\u7f6e\u5728\u8fd9\u4e2a\u7a97\u53e3\u4e2d\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u521b\u5efaListbox<\/h3>\n<\/p>\n<p><p>\u521b\u5efaListbox\u662f\u901a\u8fc7<code>tk.Listbox()<\/code>\u51fd\u6570\u5b9e\u73b0\u7684\u3002\u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u7236\u7ea7\u7ec4\u4ef6\uff08\u901a\u5e38\u662f\u4e3b\u7a97\u53e3\uff09\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u9009\u62e9\u6a21\u5f0f<\/strong>\uff1a<code>selectmode<\/code>\u9009\u9879\u5141\u8bb8\u4f60\u8bbe\u7f6eListbox\u7684\u9009\u62e9\u6a21\u5f0f\u3002\u5e38\u89c1\u7684\u6a21\u5f0f\u6709<code>tk.SINGLE<\/code>\uff08\u5355\u9009\uff09\u548c<code>tk.MULTIPLE<\/code>\uff08\u591a\u9009\uff09\u3002<\/li>\n<li><strong>\u5c3a\u5bf8\u548c\u989c\u8272<\/strong>\uff1a\u4f60\u53ef\u4ee5\u4f7f\u7528<code>width<\/code>\u548c<code>height<\/code>\u6765\u8bbe\u7f6eListbox\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c<code>bg<\/code>\u548c<code>fg<\/code>\u9009\u9879\u5206\u522b\u8bbe\u7f6e\u80cc\u666f\u8272\u548c\u524d\u666f\u8272\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u5411Listbox\u6dfb\u52a0\u9879\u76ee<\/h3>\n<\/p>\n<p><p>\u6dfb\u52a0\u9879\u76ee\u5230Listbox\u4e2d\u662f\u901a\u8fc7<code>insert<\/code>\u65b9\u6cd5\u5b9e\u73b0\u7684\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>tk.END<\/code>\u4f5c\u4e3a\u7d22\u5f15\uff0c\u5c06\u9879\u76ee\u6dfb\u52a0\u5230Listbox\u7684\u672b\u5c3e\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5faa\u73af\u6dfb\u52a0\u9879\u76ee<\/strong>\uff1a\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u9879\u76ee\u662f\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u5faa\u73af\u6dfb\u52a0\u5230Listbox\u4e2d\u7684\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5e03\u5c40\u548c\u663e\u793aListbox<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u5e76\u914d\u7f6e\u597dListbox\u540e\uff0c\u4f60\u9700\u8981\u5c06\u5176\u653e\u7f6e\u5728\u4e3b\u7a97\u53e3\u4e2d\uff0c\u4f7f\u5176\u53ef\u89c1\u3002\u8fd9\u901a\u5e38\u662f\u901a\u8fc7\u5e03\u5c40\u7ba1\u7406\u5668\u5b9e\u73b0\u7684\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528<code>pack<\/code>\u5e03\u5c40\u7ba1\u7406\u5668<\/strong>\uff1a<code>pack<\/code>\u662fTkinter\u4e2d\u5e38\u7528\u7684\u5e03\u5c40\u7ba1\u7406\u5668\u4e4b\u4e00\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>pack()<\/code>\u65b9\u6cd5\u5c06Listbox\u653e\u7f6e\u5728\u4e3b\u7a97\u53e3\u4e2d\uff0c\u5e76\u901a\u8fc7\u8bbe\u7f6e<code>pady<\/code>\u7b49\u53c2\u6570\u6765\u8c03\u6574Listbox\u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u8fd0\u884c\u4e3b\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4e3a\u4e86\u8ba9\u5e94\u7528\u7a0b\u5e8f\u4fdd\u6301\u8fd0\u884c\u72b6\u6001\uff0c\u4f60\u9700\u8981\u8c03\u7528<code>root.mainloop()<\/code>\u3002\u8fd9\u5c06\u542f\u52a8Tkinter\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u54cd\u5e94\u7528\u6237\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4ea4\u4e92\u529f\u80fd\u7684\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u5728\u57fa\u7840\u7684Listbox\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u4e3a\u5176\u6dfb\u52a0\u66f4\u591a\u7684\u4ea4\u4e92\u529f\u80fd\u3002\u8fd9\u5305\u62ec\u54cd\u5e94\u7528\u6237\u7684\u9009\u62e9\u3001\u66f4\u6539Listbox\u7684\u5185\u5bb9\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u7ed1\u5b9a\u4e8b\u4ef6<\/strong>\uff1a\u4f60\u53ef\u4ee5\u7ed1\u5b9a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u5230Listbox\u4e0a\uff0c\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u9009\u62e9\u4e00\u4e2a\u9879\u76ee\u65f6\u89e6\u53d1\u67d0\u4e2a\u52a8\u4f5c\u3002<\/li>\n<li><strong>\u52a8\u6001\u66f4\u65b0Listbox<\/strong>\uff1a\u901a\u8fc7\u63d0\u4f9b\u6309\u94ae\u6216\u5176\u4ed6\u63a7\u4ef6\uff0c\u5141\u8bb8\u7528\u6237\u52a8\u6001\u5730\u6dfb\u52a0\u6216\u5220\u9664Listbox\u4e2d\u7684\u9879\u76ee\u3002<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06Listbox\u4e0e\u5176\u4ed6\u7ec4\u4ef6\u7ed3\u5408\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u5e26\u6709Listbox\u7684\u7b80\u5355\u5e94\u7528\u7a0b\u5e8f\uff0c\u540c\u65f6\u5b9e\u73b0\u4e00\u4e9b\u57fa\u672c\u7684\u4ea4\u4e92\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def add_item():<\/p>\n<p>    new_item = entry.get()<\/p>\n<p>    if new_item:<\/p>\n<p>        listbox.insert(tk.END, new_item)<\/p>\n<p>        entry.delete(0, tk.END)<\/p>\n<p>def delete_item():<\/p>\n<p>    selected = listbox.curselection()<\/p>\n<p>    if selected:<\/p>\n<p>        listbox.delete(selected)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Listbox \u793a\u4f8b&quot;)<\/p>\n<h2><strong>\u521b\u5efaListbox<\/strong><\/h2>\n<p>listbox = tk.Listbox(root, selectmode=tk.SINGLE, width=40, height=10)<\/p>\n<p>listbox.pack(pady=10)<\/p>\n<h2><strong>\u521b\u5efa\u8f93\u5165\u6846\u548c\u6309\u94ae<\/strong><\/h2>\n<p>entry = tk.Entry(root, width=40)<\/p>\n<p>entry.pack(pady=5)<\/p>\n<p>add_button = tk.Button(root, text=&quot;Add Item&quot;, command=add_item)<\/p>\n<p>add_button.pack(side=tk.LEFT, padx=5)<\/p>\n<p>delete_button = tk.Button(root, text=&quot;Delete Item&quot;, command=delete_item)<\/p>\n<p>delete_button.pack(side=tk.RIGHT, padx=5)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7\u8f93\u5165\u6846\u548c\u6309\u94ae\u6765\u52a8\u6001\u66f4\u65b0Listbox\u7684\u5185\u5bb9\uff0c\u4ee5\u53ca\u5982\u4f55\u5220\u9664\u9009\u5b9a\u7684\u9879\u76ee\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u548c\u7ba1\u7406Tkinter\u4e2d\u7684Listbox\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Listbox\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efa\u4e00\u4e2aListbox\u3002\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165Tkinter\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e3b\u7a97\u53e3\uff0c\u63a5\u7740\u6dfb\u52a0Listbox\u63a7\u4ef6\uff0c\u5e76\u4f7f\u7528pack()\u6216grid()\u65b9\u6cd5\u6765\u663e\u793a\u5b83\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\nroot = tk.Tk()\nlistbox = tk.Listbox(root)\nlistbox.pack()\n\n# \u6dfb\u52a0\u9009\u9879\nlistbox.insert(1, &quot;\u9009\u98791&quot;)\nlistbox.insert(2, &quot;\u9009\u98792&quot;)\nlistbox.insert(3, &quot;\u9009\u98793&quot;)\n\nroot.mainloop()\n<\/code><\/pre>\n<p><strong>Listbox\u53ef\u4ee5\u7528\u4e8e\u54ea\u4e9b\u573a\u666f\uff1f<\/strong><br \/>Listbox\u9002\u7528\u4e8e\u8bb8\u591a\u573a\u666f\uff0c\u6bd4\u5982\u9009\u62e9\u591a\u4e2a\u9879\u76ee\u3001\u663e\u793a\u5217\u8868\u6570\u636e\u3001\u63d0\u4f9b\u9009\u9879\u4f9b\u7528\u6237\u9009\u62e9\u7b49\u3002\u5b83\u5728\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u80fd\u591f\u5e2e\u52a9\u7528\u6237\u4ee5\u76f4\u89c2\u7684\u65b9\u5f0f\u8fdb\u884c\u9009\u62e9\u3002<\/p>\n<p><strong>\u5982\u4f55\u4eceListbox\u4e2d\u83b7\u53d6\u7528\u6237\u9009\u62e9\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u83b7\u53d6\u7528\u6237\u5728Listbox\u4e2d\u9009\u62e9\u7684\u5185\u5bb9\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>curselection()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6240\u9009\u9879\u7684\u7d22\u5f15\uff0c\u7136\u540e\u4f7f\u7528<code>get()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5bf9\u5e94\u7684\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">selected_index = listbox.curselection()\nselected_value = listbox.get(selected_index)\nprint(f&quot;\u7528\u6237\u9009\u62e9\u7684\u9879\u662f: {selected_value}&quot;)\n<\/code><\/pre>\n<p><strong>Listbox\u7684\u6837\u5f0f\u548c\u529f\u80fd\u53ef\u4ee5\u5982\u4f55\u81ea\u5b9a\u4e49\uff1f<\/strong><br \/>Listbox\u7684\u6837\u5f0f\u548c\u529f\u80fd\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u81ea\u5b9a\u4e49\u3002\u53ef\u4ee5\u8bbe\u7f6e\u80cc\u666f\u8272\u3001\u5b57\u4f53\u3001\u9009\u62e9\u6a21\u5f0f\uff08\u5355\u9009\u6216\u591a\u9009\uff09\u7b49\u3002\u8fd8\u53ef\u4ee5\u7ed1\u5b9a\u4e8b\u4ef6\uff0c\u5982\u5355\u51fb\u6216\u53cc\u51fb\uff0c\u4ee5\u54cd\u5e94\u7528\u6237\u64cd\u4f5c\u3002\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u5982\u4f55\u6539\u53d8\u80cc\u666f\u8272\u548c\u5b57\u4f53\uff1a  <\/p>\n<pre><code class=\"language-python\">listbox.config(bg=&quot;lightblue&quot;, font=(&quot;Arial&quot;, 12))\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u521b\u5efaListbox\u7684\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528Tkinter\u5e93\u3002Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":961116,"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\/961106"}],"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=961106"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/961106\/revisions"}],"predecessor-version":[{"id":961119,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/961106\/revisions\/961119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/961116"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=961106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=961106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=961106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}