{"id":7988,"date":"2020-08-25T21:25:16","date_gmt":"2020-08-25T21:25:16","guid":{"rendered":"https:\/\/www.askpython.com\/?p=7988"},"modified":"2020-08-25T21:25:18","modified_gmt":"2020-08-25T21:25:18","slug":"tkinter-listbox-option-menu","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-listbox-option-menu","title":{"rendered":"Tkinter Listbox and Option Menu &#8211; How to Use the Listbox and Option Menu Widgets?"},"content":{"rendered":"\n<p>Hey guys, welcome to this article on Tkinter Listbox and Option menu using Tkinter. I will walk you through some examples for the same.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Listbox?<\/h2>\n\n\n\n<p>The <strong>Listbox<\/strong> widget in Tkinter is widely used to display a set of items to the user. The user can select from these items.<\/p>\n\n\n\n<p>We have the basic starter code that we always get, a 400 x 400 and we have got an icon for the title in there.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom tkinter import *\n\nroot=Tk()\nroot.title(&quot;Listbox demo&quot;)\nroot.geometry(&quot;400x480&quot;)\n\nroot.mainloop()\n<\/pre><\/div>\n\n\n<p>So to create a list box, you just define it like always and a list box is just a type of widget with tkinter.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create your first Tkinter listbox widget<\/h2>\n\n\n\n<p>Let&#8217;s create a list box, we\u2019ll call it <code>my_listbox<\/code> and set that equal to a <code>Listbox()<\/code> and put it in <code>root<\/code>.  <\/p>\n\n\n\n<p>We will pack this on the screen and let&#8217;s give this a <code>pack()<\/code> and give it a \u201cpady\u201d of 15.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#Listbox\nmy_listbox=Listbox(root)\nmy_listbox.pack(pady=15)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Adding items to the listbox individually<\/h3>\n\n\n\n<p>Now, in order to put things in, you could do it in a couple of different ways. You can just do it manually or you could create a list and loop through the list and put each of those things in it one at a time. <\/p>\n\n\n\n<p>We will be using <code>my_listbox.insert<\/code> method. This method takes two parameters. There&#8217;s an index and a string. <\/p>\n\n\n\n<p>So the index is the index number, the item in our list box, the position in which we want to put it in and the first one is \u20180\u2019. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Add item to listbox\n\nmy_listbox.insert(END, &quot;First&quot;)  \nmy_listbox.insert(END, &quot;Second&quot;)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Adding mutliple items to a Tkinter listbox using loops<\/h3>\n\n\n\n<p>Let&#8217;s add a list of items. So I&#8217;m going to just <code>create my_list<\/code> and it&#8217;s just going to be a <a href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" class=\"rank-math-link\">python list <\/a>and we can put anything we want in here. So I&#8217;m just going to insert \u201cone\u201d, \u201ctwo\u201d and \u201cthree\u201d and that&#8217;s fine. Now we can just <a href=\"https:\/\/www.askpython.com\/python\/python-loops-in-python\" class=\"rank-math-link\">loop <\/a>through the list and put each item in there. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmy_list = &#x5B;&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;]\n\nfor item in my_list:\n\tmy_listbox.insert(END, item)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Adding Tkinter Buttons to Delete Listbox Items<\/strong><\/h3>\n\n\n\n<p>Now that we know how to add listbox items, let&#8217;s create a <a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-buttons\" class=\"rank-math-link\">Tkinter button<\/a> to delete the items.<\/p>\n\n\n\n<p>If we click on one of these items they get highlighted, so let&#8217;s create a button. We will call it my_button and that\u2019s going to be in root and pack <code>my_button<\/code> with a pady of 10. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmy_button = Button(root, text=&quot;Delete&quot;, command=delete)     \nmy_button.pack(pady=10)\n<\/pre><\/div>\n\n\n<p>Now, we need to create a <code>delete<\/code> function. When something is highlighted in your list box after you&#8217;ve clicked on it, it becomes the anchor. So, we want to delete the ANCHOR. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef delete():\n    my_listbox.delete(ANCHOR)\n    my_label.config(text=&quot; &quot;)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">The complete code for Tkinter Listbox widget implementation<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom tkinter import *\n\nroot=Tk()\nroot.title(&quot;Listbox demo&quot;)\nroot.geometry(&quot;400x480&quot;)\n\n#Listbox\nmy_listbox=Listbox(root)\nmy_listbox.pack(pady=15)\n\n# Add item to listbox\n\nmy_listbox.insert(END, &quot;First&quot;)  \nmy_listbox.insert(END, &quot;Second&quot;)\n\nmy_list = &#x5B;&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;]\n\nfor item in my_list:\n    my_listbox.insert(END, item)\n\ndef delete():\n    my_listbox.delete(ANCHOR)\n    my_label.config(text=&quot; &quot;)\n\nmy_button = Button(root, text=&quot;Delete&quot;, command=delete)     \nmy_button.pack(pady=10)\n\nglobal my_label\n\nmy_label=Label(root, text=&quot; &quot;)\nmy_label.pack(pady=5)\n\nroot.mainloop()\n<\/pre><\/div>\n\n\n<p>The output of the above code is as shown below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/List-item.png\" alt=\"List Item\" class=\"wp-image-8112\" width=\"383\" height=\"494\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/List-item.png 499w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/List-item-233x300.png 233w\" sizes=\"auto, (max-width: 383px) 100vw, 383px\" \/><figcaption>List Item<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">What is an Option menu?<\/h2>\n\n\n\n<p>The <strong>OptionMenu<\/strong> class is a helper class that creates a popup menu, and a button to display it. This widget generates a drop down list with many option values. <\/p>\n\n\n\n<p>Let&#8217;s create a simple Option Menu structure. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create your first Tkinter Option menu<\/h2>\n\n\n\n<p>To create an options menu based on the dropdown menu. The first step for this is to list out Basic security questions. Pass them into an options menu and create an entry for an answer. <\/p>\n\n\n\n<p>First, we will create list of questions.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nQuestion = &#x5B;\u201cWhat is your mother\u2019s maiden name?\u201d,\n                    \u201c Who is your favorite author?\u201d,\n                    \u201c What was your first pets name? \u201c,\n                     \u201cWhat street did you grow up on? \u201c\n] \n<\/pre><\/div>\n\n\n<p>Now we have to pass these through a variable tkvarq. To understand creation of tk variable, the first alternate to pass barrier is root and set the questions for this variable using the set().<\/p>\n\n\n\n<p>This is a string variable StringVar , we pass the questions into it as shown below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntkvarq = StringVar(root) \ntkvarq.set(questions&#x5B;0])\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Creating the Tkinter OptionMenu object<\/h3>\n\n\n\n<p>The questions are displayed by creating an object of <code>OptionMenu<\/code> and the answers can be entered on the <code>answer_entry<\/code> text box created. This text box has been created using the <code>Entry<\/code> class.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nquestion_menu = OptionMenu(root,  tkvarq, *questions)\nquestion_menu.pack()\n\n#Answer entry\nanswer_entry = Entry(root, width=30)\nanswer_entry.pack()\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">The complete code for Tkinter OptionMenu widget implementation<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom tkinter import *\nroot = Tk()\nroot.title(\u201cOption Menu\u201d)\nroot.geometry(\u2018700x500\u2019)\n\n# Create the list of questions\n# Pass them into an option menu\n# Create an entry for the answer\n# Create submit button\n\ndef print_answers():\n       print (\u201cQ: {}    A:  {} \u201c,format(tkvarq.get(),  answer_entry.get()))\n       return None \n\nQuestion = &#x5B;\u201cWhat is your mother\u2019s maiden name?\u201d,\n                    \u201c Who is your favorite author?\u201d,\n                    \u201c What was your first pets name? \u201c,\n                     \u201cWhat street did you grow up on? \u201c\n] \n\ntkvarq = StringVar(root) \ntkvarq.set(questions&#x5B;0])\nquestion_menu = OptionMenu(root,  tkvarq, *questions)\nquestion_menu.pack()\n\n#Answer entry\nanswer_entry = Entry(root, width=30)\nanswer_entry.pack()\n\n#Submit button\nsubmit_button = Button(root, test=\u2019Submit\u2019,  command=print_answers)\nsubmit_button.pack()\n\nroot.mainloop()\n<\/pre><\/div>\n\n\n<p>This code generates an options menu which contains the questions. You can choose one question that you wish to answer and write your answer for it in the text box provided. <\/p>\n\n\n\n<p>Kindly note, this code does not contain the validations, meaning it will not tell you if the answer entered is correct or not.<\/p>\n\n\n\n<p>The output of the above code is as shown below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Option-Menu-edited.png\" alt=\"Option Menu\" class=\"wp-image-8113\" width=\"460\" height=\"303.72460496613996\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Option-Menu-edited.png 886w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Option-Menu-edited-300x198.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/Option-Menu-edited-768x506.png 768w\" sizes=\"(max-width: 886px) 100vw, 886px\" \/><figcaption>Option Menu<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This comes to the end of our tutorial on the Tkinter Listbox and option menu. Do try out the examples shown and give your feedback in the comment section below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey guys, welcome to this article on Tkinter Listbox and Option menu using Tkinter. I will walk you through some examples for the same. What is a Listbox? The Listbox widget in Tkinter is widely used to display a set of items to the user. The user can select from these items. We have the [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":8115,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92],"tags":[],"class_list":["post-7988","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tkinter"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=7988"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7988\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/8115"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=7988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=7988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=7988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}