{"id":1347,"date":"2020-11-30T08:10:34","date_gmt":"2020-11-30T08:10:34","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1347"},"modified":"2025-04-04T04:44:28","modified_gmt":"2025-04-04T04:44:28","slug":"tkinter-listbox","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-listbox\/","title":{"rendered":"Tkinter Listbox"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Tkinter <code>Listbox<\/code> widget to display a list of items.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-tkinter-listbox'>Introduction to the Tkinter Listbox <a href=\"#introduction-to-the-tkinter-listbox\" class=\"anchor\" id=\"introduction-to-the-tkinter-listbox\" title=\"Anchor for Introduction to the Tkinter Listbox\">#<\/a><\/h2>\n\n\n\n<p>A <code>Listbox<\/code> widget displays a list of single-line text items. A <code>Listbox<\/code> allows you to browse through the items and select one or multiple items at once.<\/p>\n\n\n\n<p>To create a Listbox widget, you follow these steps:<\/p>\n\n\n\n<p>First, import the <code>tkinter<\/code> as the <code>tk<\/code> module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, create a listbox widget using <code>Listbox<\/code> constructor:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tk.Listbox(master=<span class=\"hljs-literal\">None<\/span>, cnf={}, **kw)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>master<\/code>: The window or frame where you want to place the <code>Listbox<\/code> widget.<\/li>\n\n\n\n<li><code>cnf<\/code> : a dictionary that specifies the configurations of the <code>Listbox<\/code> widget.<\/li>\n\n\n\n<li><code>**kw<\/code> one or more keyword arguments that specify additional configuration for the widget.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='adding-list-items'>Adding List Items <a href=\"#adding-list-items\" class=\"anchor\" id=\"adding-list-items\" title=\"Anchor for Adding List Items\">#<\/a><\/h2>\n\n\n\n<p>To add items to a Listbox widget: <\/p>\n\n\n\n<p>First, create a <code>Variable<\/code> object and initialize its value to the list of items: <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">list_items = tk.Variable(value=items)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, assign this <code>Variable<\/code> object to the <code>listvariable<\/code> option of the <code>Listbox<\/code> widget:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tk.Listbox(listvariable=list_items)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can modify the <code>list_items<\/code> variable to manipulate list items such as adding, changing, and removing.<\/p>\n\n\n\n<p>The following program create a Listbox widget:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n\n<span class=\"hljs-comment\"># create the main window<\/span>\nroot = tk.Tk()\nroot.geometry(<span class=\"hljs-string\">'400x180'<\/span>)\nroot.title(<span class=\"hljs-string\">'Listbox'<\/span>)\n\n<span class=\"hljs-comment\"># create a variabe object<\/span>\nlanguages = (<span class=\"hljs-string\">'Java'<\/span>, <span class=\"hljs-string\">'C'<\/span>, <span class=\"hljs-string\">'C++'<\/span>, <span class=\"hljs-string\">'C#'<\/span>, <span class=\"hljs-string\">'Python'<\/span>,\n         <span class=\"hljs-string\">'Go'<\/span>, <span class=\"hljs-string\">'JavaScript'<\/span>, <span class=\"hljs-string\">'PHP'<\/span>, <span class=\"hljs-string\">'Swift'<\/span>)\n\nlist_variable = tk.Variable(value=languages)\n\n\n<span class=\"hljs-comment\"># label<\/span>\nlabel = ttk.Label(\n    root, \n    text=<span class=\"hljs-string\">'Select your favorite programming languages:'<\/span>\n)\nlabel.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">0<\/span>, side=tk.TOP, fill=tk.X)\n\nlistbox = tk.Listbox(\n    root,\n    listvariable=list_variable,\n    height=<span class=\"hljs-number\">6<\/span>,\n)\n\nlistbox.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, expand=<span class=\"hljs-literal\">True<\/span>, fill=tk.BOTH, side=tk.LEFT)\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"406\" height=\"216\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/Tkinter-listbox.png\" alt=\"\" class=\"wp-image-7478\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/Tkinter-listbox.png 406w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/Tkinter-listbox-300x160.png 300w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/figure>\n\n\n\n<p>How it works:<\/p>\n\n\n\n<p>First, create a <code>Variable<\/code> object and initialize its value to a tuple:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># create a variabe object<\/span>\nlanguages = (<span class=\"hljs-string\">'Java'<\/span>, <span class=\"hljs-string\">'C'<\/span>, <span class=\"hljs-string\">'C++'<\/span>, <span class=\"hljs-string\">'C#'<\/span>, <span class=\"hljs-string\">'Python'<\/span>,\n         <span class=\"hljs-string\">'Go'<\/span>, <span class=\"hljs-string\">'JavaScript'<\/span>, <span class=\"hljs-string\">'PHP'<\/span>, <span class=\"hljs-string\">'Swift'<\/span>)\n\nlist_variable = tk.Variable(value=languages)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, create a <code>Listbox<\/code> widget and assign the <code>list_variable<\/code> to the <code>listvariable<\/code> option:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">listbox = tk.Listbox(\n    root,\n    listvariable=list_variable,\n    height=<span class=\"hljs-number\">6<\/span>\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-pack\/\">pack<\/a> the <code>Listbox<\/code>widget on the main window:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">listbox.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, expand=<span class=\"hljs-literal\">True<\/span>, fill=tk.BOTH, side=tk.LEFT)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='setting-a-selection-mode'>Setting a Selection Mode <a href=\"#setting-a-selection-mode\" class=\"anchor\" id=\"setting-a-selection-mode\" title=\"Anchor for Setting a Selection Mode\">#<\/a><\/h2>\n\n\n\n<p>The <code>selectmode<\/code> option determines how many items you can select and how the mouse drags will affect the items:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tk.BROWSE<\/code> &#8211; allows a single selection. If you select an item and drag it to a different line, the selection will follow the mouse. This is the default.<\/li>\n\n\n\n<li><code>tk.EXTENDED<\/code> &#8211; select any adjacent group of items at once by clicking the first item and dragging to the last line.<\/li>\n\n\n\n<li><code>tk.SINGLE<\/code> &#8211; allow you to select one line and you cannot drag the mouse.<\/li>\n\n\n\n<li><code>tk.MULTIPLE<\/code> &#8211; select any number of lines at once. Clicking on any line toggles whether it is selected or not.<\/li>\n<\/ul>\n\n\n\n<p>The following program sets the selection mode of the Listbox widget to <code>tk.MULTIPLE<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n\n<span class=\"hljs-comment\"># create the main window<\/span>\nroot = tk.Tk()\nroot.geometry(<span class=\"hljs-string\">'400x180'<\/span>)\nroot.title(<span class=\"hljs-string\">'Listbox'<\/span>)\n\n<span class=\"hljs-comment\"># create a variabe object<\/span>\nlanguages = (<span class=\"hljs-string\">'Java'<\/span>, <span class=\"hljs-string\">'C'<\/span>, <span class=\"hljs-string\">'C++'<\/span>, <span class=\"hljs-string\">'C#'<\/span>, <span class=\"hljs-string\">'Python'<\/span>,\n         <span class=\"hljs-string\">'Go'<\/span>, <span class=\"hljs-string\">'JavaScript'<\/span>, <span class=\"hljs-string\">'PHP'<\/span>, <span class=\"hljs-string\">'Swift'<\/span>)\n\nlist_variable = tk.Variable(value=languages)\n\n\n<span class=\"hljs-comment\"># label<\/span>\nlabel = ttk.Label(\n    root, \n    text=<span class=\"hljs-string\">'Select your favorite programming languages:'<\/span>\n)\nlabel.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">0<\/span>, side=tk.TOP, fill=tk.X)\n\nlistbox = tk.Listbox(\n    root,\n    listvariable=list_variable,\n    height=<span class=\"hljs-number\">6<\/span>,\n    selectmode=tk.MULTIPLE,\n)\n\nlistbox.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, expand=<span class=\"hljs-literal\">True<\/span>, fill=tk.BOTH, side=tk.LEFT)\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we set the select mode to <code>tk.MULTIPLE<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">listbox = tk.Listbox(\n    root,\n    listvariable=list_variable,\n    height=<span class=\"hljs-number\">6<\/span>,\n    selectmode=tk.MULTIPLE,\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='binding-the-selected-event'>Binding the selected event <a href=\"#binding-the-selected-event\" class=\"anchor\" id=\"binding-the-selected-event\" title=\"Anchor for Binding the selected event\">#<\/a><\/h2>\n\n\n\n<p>If you want to execute a function automatically when users select a list item, you can bind that function to the <code>&lt;&lt;ListboxSelect>><\/code> event:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">listbox.bind(<span class=\"hljs-string\">'&lt;&lt;ListboxSelect&gt;&gt;'<\/span>, callback)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> tkinter.messagebox <span class=\"hljs-keyword\">import<\/span> showinfo\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n\n<span class=\"hljs-comment\"># create the main window<\/span>\nroot = tk.Tk()\nroot.geometry(<span class=\"hljs-string\">'400x180'<\/span>)\nroot.title(<span class=\"hljs-string\">'Listbox'<\/span>)\n\n<span class=\"hljs-comment\"># create a variabe object<\/span>\nlanguages = (<span class=\"hljs-string\">'Java'<\/span>, <span class=\"hljs-string\">'C'<\/span>, <span class=\"hljs-string\">'C++'<\/span>, <span class=\"hljs-string\">'C#'<\/span>, <span class=\"hljs-string\">'Python'<\/span>,\n         <span class=\"hljs-string\">'Go'<\/span>, <span class=\"hljs-string\">'JavaScript'<\/span>, <span class=\"hljs-string\">'PHP'<\/span>, <span class=\"hljs-string\">'Swift'<\/span>)\n\nlist_variable = tk.Variable(value=languages)\n\n\n<span class=\"hljs-comment\"># label<\/span>\nlabel = ttk.Label(\n    root, \n    text=<span class=\"hljs-string\">'Select your favorite programming languages:'<\/span>\n)\nlabel.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">0<\/span>, side=tk.TOP, fill=tk.X)\n\nlistbox = tk.Listbox(\n    root,\n    listvariable=list_variable,\n    height=<span class=\"hljs-number\">6<\/span>,\n    selectmode=tk.MULTIPLE,\n)\n\nlistbox.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, expand=<span class=\"hljs-literal\">True<\/span>, fill=tk.BOTH, side=tk.LEFT)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">handle_item_select<\/span><span class=\"hljs-params\">(event)<\/span>:<\/span>\n    selected_indices = listbox.curselection()\n    selected_languages = <span class=\"hljs-string\">\",\"<\/span>.join(&#91;listbox.get(i) <span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> selected_indices])\n\n    showinfo(\n        title=<span class=\"hljs-string\">'Information'<\/span>, \n        message=<span class=\"hljs-string\">f'You selected: <span class=\"hljs-subst\">{selected_languages}<\/span>'<\/span>\n    )\n\nlistbox.bind(<span class=\"hljs-string\">'&lt;&lt;ListboxSelect&gt;&gt;'<\/span>, handle_item_select)\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"504\" height=\"365\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/Tkinter-listbox-item-select.png\" alt=\"Tkinter listbox item select\" class=\"wp-image-7477\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/Tkinter-listbox-item-select.png 504w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/Tkinter-listbox-item-select-300x217.png 300w\" sizes=\"auto, (max-width: 504px) 100vw, 504px\" \/><\/figure>\n\n\n\n<p>First, define a function <code>handle_item_select<\/code> to display selected items:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">handle_item_select<\/span><span class=\"hljs-params\">(event)<\/span>:<\/span>\n    selected_indices = listbox.curselection()\n    selected_languages = <span class=\"hljs-string\">\",\"<\/span>.join(&#91;listbox.get(i) <span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> selected_indices])\n\n    showinfo(\n        title=<span class=\"hljs-string\">'Information'<\/span>, \n        message=<span class=\"hljs-string\">f'You selected: <span class=\"hljs-subst\">{selected_languages}<\/span>'<\/span>\n    )<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the function:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Get the currently selected list items using the <code>curselection()<\/code> method of the listbox instance.<\/li>\n\n\n\n<li>Concatenate selected items into a single string.<\/li>\n\n\n\n<li>Display the result string in a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-messagebox\/\">message box<\/a>.<\/li>\n<\/ul>\n\n\n\n<p>Second, bind the <code>handle_item_select<\/code> function to the item select event:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">listbox.bind(<span class=\"hljs-string\">'&lt;&lt;ListboxSelect&gt;&gt;'<\/span>, handle_item_select)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='adding-a-scrollbar-to-the-listbox'>Adding a scrollbar to the Listbox <a href=\"#adding-a-scrollbar-to-the-listbox\" class=\"anchor\" id=\"adding-a-scrollbar-to-the-listbox\" title=\"Anchor for Adding a scrollbar to the Listbox\">#<\/a><\/h2>\n\n\n\n<p>The following program illustrates how to add a scrollbar to a Listbox:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> tkinter.messagebox <span class=\"hljs-keyword\">import<\/span> showinfo\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n\n<span class=\"hljs-comment\"># create the main window<\/span>\nroot = tk.Tk()\nroot.geometry(<span class=\"hljs-string\">'400x180'<\/span>)\nroot.title(<span class=\"hljs-string\">'Listbox'<\/span>)\n\n<span class=\"hljs-comment\"># create a variabe object<\/span>\nlanguages = (<span class=\"hljs-string\">'Java'<\/span>, <span class=\"hljs-string\">'C'<\/span>, <span class=\"hljs-string\">'C++'<\/span>, <span class=\"hljs-string\">'C#'<\/span>, <span class=\"hljs-string\">'Python'<\/span>,\n         <span class=\"hljs-string\">'Go'<\/span>, <span class=\"hljs-string\">'JavaScript'<\/span>, <span class=\"hljs-string\">'PHP'<\/span>, <span class=\"hljs-string\">'Swift'<\/span>)\n\nlist_variable = tk.Variable(value=languages)\n\n\n<span class=\"hljs-comment\"># label<\/span>\nlabel = ttk.Label(\n    root, \n    text=<span class=\"hljs-string\">'Select your favorite programming languages:'<\/span>\n)\nlabel.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">0<\/span>, side=tk.TOP, fill=tk.X)\n\nlistbox = tk.Listbox(\n    root,\n    listvariable=list_variable,\n    height=<span class=\"hljs-number\">6<\/span>,\n    selectmode=tk.MULTIPLE,\n)\n\nlistbox.pack(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, expand=<span class=\"hljs-literal\">True<\/span>, fill=tk.BOTH, side=tk.LEFT)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">handle_item_select<\/span><span class=\"hljs-params\">(event)<\/span>:<\/span>\n    selected_indices = listbox.curselection()\n    selected_languages = <span class=\"hljs-string\">\",\"<\/span>.join(&#91;listbox.get(i) <span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> selected_indices])\n\n    showinfo(\n        title=<span class=\"hljs-string\">'Information'<\/span>, \n        message=<span class=\"hljs-string\">f'You selected: <span class=\"hljs-subst\">{selected_languages}<\/span>'<\/span>\n    )\n\nlistbox.bind(<span class=\"hljs-string\">'&lt;&lt;ListboxSelect&gt;&gt;'<\/span>, handle_item_select)\n\n\n<span class=\"hljs-comment\"># link a scrollbar to a list<\/span>\nv_scrollbar = ttk.Scrollbar(\n    root,\n    orient=tk.VERTICAL,\n    command=listbox.yview\n)\n\nlistbox&#91;<span class=\"hljs-string\">'yscrollcommand'<\/span>] = v_scrollbar.set\nv_scrollbar.pack(pady=<span class=\"hljs-number\">10<\/span>, side=tk.RIGHT, fill=tk.Y)\n\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"406\" height=\"216\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/Tkinter-listbox-scrollbar.png\" alt=\"Tkinter listbox scrollbar\" class=\"wp-image-7476\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/Tkinter-listbox-scrollbar.png 406w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/Tkinter-listbox-scrollbar-300x160.png 300w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/figure>\n\n\n\n<p>First, create a <code>Scrollbar<\/code> widget and link it to the <code>Listbox<\/code> widget:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">v_scrollbar = ttk.Scrollbar(\n    root,\n    orient=tk.VERTICAL,\n    command=listbox.yview\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, allow the <code>Listbox<\/code> to communicate its scroll state to the <code>Scrollbar<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">listbox&#91;<span class=\"hljs-string\">'yscrollcommand'<\/span>] = v_scrollbar.set<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For more information on how to link a scrollbar to a scrollable widget, check out the <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-scrollbar\/\">scrollbar widget tutorial<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>tk.Listbox(container, height, listvariable)<\/code> to create a Listbox widget; a <code>listvariable<\/code> should be a <code>tk.StringVar(value=items)<\/code>.<\/li>\n\n\n\n<li>Bind a callback function to the <code>'&lt;&lt;ListboxSelect>>'<\/code> event to execute the function when one or more list items are selected.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1347\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-listbox\/\"\n\t\t\t\tdata-post-title=\"Tkinter Listbox\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1347\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-listbox\/\"\n\t\t\t\tdata-post-title=\"Tkinter Listbox\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the Tkinter Listbox widget to display a list of items.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":16,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1347","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=1347"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1347\/revisions"}],"predecessor-version":[{"id":7479,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1347\/revisions\/7479"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1232"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=1347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}