Python Tkinter List Box


This Article covers the use of the Python Tkinter List box.

What is the Python Tkinter List Box?

A Python Tkinter widget that is used to display a list of options for the user to select. A List Box displays all of its options at once, unlike the ComboBox for instance. Furthermore, all options are in text format.

List Box Syntax

listbox = Listbox(root, option........)  

Options

No.OptionDescription
1bgBackground color for the area around the widget.
2bdSize of the border around the widget. Default value is 2 pixels.
3cursorWhen the mouse is hovering over this widget, it can be changed to a special cursor type like an arrow or dot.
4fontThe type of font to be used for this widget.
5fgThe color for the text.
6heightNumber of lines displayed in the List box. Defaut value is 10.
7highlightcolorThe text color when the widget is under focus.
8reliefIt specifies the type of the border. Default is Flat, other options include RAISED and SUNKEN.
9selectbackgroundBackground color for text that has been selected.
10selectmodeIt determines the number of items that can be selected from the list and the effects of the mouse on this selection. It can set to BROWSE, SINGLE, MULTIPLE, EXTENDED.
11widthThe width of the widget in characters.
12xscrollcommandAllows the user to scroll horizontally.
13 yscrollcommandAllows the user to scroll vertically.

List Box Example 1.

Below is a demonstration on how to create a Tkinter List box in Python. It’s syntax is relatively simple. Only the List box object needs to be created and you can keep adding items into it using the insert function.

from tkinter import *  
  
root = Tk()
root.geometry("200x220")
frame = Frame(root)
frame.pack()

label = Label(root,text = "A list of Grocery items.")  
label.pack()  
  
listbox = Listbox(root)  
  
listbox.insert(1,"Bread")  
listbox.insert(2, "Milk")  
listbox.insert(3, "Meat")  
listbox.insert(4, "Cheese")
listbox.insert(5, "Vegetables")  
  
listbox.pack()  
root.mainloop()
Tkinter Widget - List box

List Box Example 2.

In this example we’ll demonstrate how to retrieve the selected List box values. The curselection() returns the index of the item that is currently selected. Remember that indexing starts from 0.

from tkinter import *  

def retrieve():
    print(listbox.curselection())
  
root = Tk()
root.geometry("200x220")
frame = Frame(root)
frame.pack()

label = Label(root,text = "A list of Grocery items.")  
label.pack()  
  
listbox = Listbox(root)  
  
listbox.insert(1,"Bread")  
listbox.insert(2, "Milk")  
listbox.insert(3, "Meat")  
listbox.insert(4, "Cheese")
listbox.insert(5, "Vegetables")  
listbox.pack()

bttn = Button(frame, text = "Submit", command = retrieve)
bttn.pack(side= "bottom")
root.mainloop()  

Furthermore, you can print out a list of all the items in the list box by using the get() function on the list box object. The get() function takes two inputs, a start and an end point. By using the following code, the get() function will return a list of all the items.

result = listbox.get(0 , END)
print(result)

That concludes our tutorial on the Python Tkinter List Box. You can head back to the main Tkinter article using this link. Any Questions regarding the above content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments