ListBox is one of the many useful widgets provided by Tkinter for GUI development. The Listbox widget is used to display a list of items from which a user can select one or more items according to the constraints. In this article, we’ll see how we can get the selected value(s) from a Listbox widget.
The curselection method on listbox returns a tuple containing the indices/line numbers of the selected item(s) of the listbox, starting from 0. The selected_item function that we made, traverses the tuple returned by the curselection method and prints the corresponding item of the listbox using the indices. It is executed when we press the “Print Selected” button. In the case of no selected items, curselection method returns an empty tuple.
Note: You can change the selectmode parameter of the listbox widget to “SINGLE” for putting a constraint of choosing a single value only.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.