Back to: Python Programming
The tkinter Listbox widget is a standard GUI element used to display a list of textual options from which a user can select one or more items. It does not natively support images, multiple columns, or built-in scrollbars.

Key Concepts & Usage
- Creation: Create a listbox using the
Listbox()constructor, specifying a parent window and optional configurations. - Adding Items: Use the
.insert()method to add items..ENDis a common index to append items to the end of the list. - Selection Modes: The
selectmodeoption determines how many items a user can select:.BROWSE(default): Only one item can be selected at a time, and the selection follows the mouse if dragged..SINGLE: Similar toBROWSE, but dragging the mouse doesn’t change the selection; the selection is set on the clicked item..MULTIPLE: Any number of items can be selected by clicking..EXTENDED: Allows selection of multiple, adjacent items by clicking and dragging (or using shift/control keys).
- Retrieving Selection: The
.curselection()method returns a tuple of the indices of the selected items. To get the actual text, you iterate through the indices and use the.get()method.
Key Configurations and Methods
| Configuration/Method | Description |
|---|---|
selectmode | Determines the selection behavior. Options include tk.BROWSE (default, single selection with dragging), tk.SINGLE (single selection, no dragging), tk.MULTIPLE (multiple selections by clicking), and tk.EXTENDED (multiple selections with dragging/range selection). |
.insert(index, *elements) | Inserts new items at a specific index (e.g., tk.END to append to the end). |
.delete(first, last=None) | Deletes items within a specified range. Use delete(0, tk.END) to clear all items. |
.curselection() | Current Selection – Returns a tuple of indices (0-based line numbers) for the currently selected item(s). |
.get(first, last=None) | Retrieves the text of the item(s) at the specified index or range. |
.bind('<<ListboxSelect>>', callback) | Binds a function to the event that occurs whenever the selection changes, which is the recommended way to track selection changes. |
Utilising Listbox
We’ll make a meal selection menu:
from tkinter import *
window = Tk()
myList = Listbox(window)
myList.pack()
myList.insert(1,"Pizza")
myList.insert(2,"Pasta")
myList.insert(3,"Schnitzel")
myList.insert(4,"Fish")
myList.insert(5,"Lasagne")
window.mainloop()
We can add colour and styling to our list:
window = Tk()
myList = Listbox(window,
bg="#f7ffde", # Sets background colour
font=("Constantia",35), # changes font face and font size
height=8, # sets the list to 8 rows
width=12 # sets the width to 12 columns
)
myList.pack()
myList.insert(1,"Pizza")
myList.insert(2,"Pasta")
myList.insert(3,"Schnitzel")
myList.insert(4,"Fish")
myList.insert(5,"Lasagne")
window.mainloop()

We can set the height of the Listbox() to precisely match the number of list items in the list with a single line:
window = Tk()
myList = Listbox(window,
bg="#f7ffde", # Sets background colour
font=("Constantia",35), # changes font face and font size
width=12 # sets the width to 12 columns
)
myList.pack()
myList.insert(1,"Pizza")
myList.insert(2,"Pasta")
myList.insert(3,"Schnitzel")
myList.insert(4,"Fish")
myList.insert(5,"Lasagne")
myList.config(height=myList.size()) # will dynamically set the height
window.mainloop()
The next thing we’ll want to do is make a selection and SUBMIT an option to order it:
from tkinter import *
# Add SUBMIT function to choose a list item
def submit():
print("You have ordered: ")
print(myList.get(myList.curselection()))
window = Tk()
myList = Listbox(window,
bg="#f7ffde",
font=("Constantia",35),
width=12
)
myList.pack()
myList.insert(1,"Pizza")
myList.insert(2,"Pasta")
myList.insert(3,"Schnitzel")
myList.insert(4,"Fish")
myList.insert(5,"Lasagne")
myList.config(height=myList.size()) # dynamically sets the height
# Add SUBMIT button to choose a list item - this the runs the "submit" function
submitButton = Button(window,text="Submit",command=submit)
submitButton.pack()
window.mainloop()
Now we’ll add a feature to insert another item to the listbox after the program is compiled and already running, so we will create an entry box within our window where we can add a custom item to this menu:
from tkinter import *
def submit():
print("You have ordered: ")
print(myList.get(myList.curselection()))
def addItem():
myList.insert(myList.size(),entryBox.get())
# myList.size gets the next list number
myList.config(height=myList.size()) # dynamically reset the height
window = Tk()
myList = Listbox(window,
bg="#f7ffde",
font=("Constantia",35),
width=12
)
myList.pack()
myList.insert(1,"Pizza")
myList.insert(2,"Pasta")
myList.insert(3,"Schnitzel")
myList.insert(4,"Fish")
myList.insert(5,"Lasagne")
myList.config(height=myList.size()) # We leave this line for initial dynamic height setting
# Adds an ENTRY BOX (text field) for new Items to by typed in
entryBox = Entry(window)
entryBox.pack()
# Adds the ADD button to run the ADD function "addItem" at the top of this code
addButton = Button(window,text="Add",command=addItem)
addButton.pack()
submitButton = Button(window,text="Submit",command=submit)
submitButton.pack()
window.mainloop()
To finish up we can create a DELETE button to remove an item from the menu.
from tkinter import *
def submit():
print("You have ordered: ")
print(myList.get(myList.curselection()))
def addItem():
myList.insert(myList.size(),entryBox.get())
# myList.size gets the next list number
myList.config(height=myList.size()) # will dynamically set the height to show new item
def deleteItem():
myList.delete(myList.curselection())
myList.config(height=myList.size()) # will dynamically reset the height as it shrinks
window = Tk()
myList = Listbox(window,
bg="#f7ffde",
font=("Constantia",35),
width=12
)
myList.pack()
myList.insert(1,"Pizza")
myList.insert(2,"Pasta")
myList.insert(3,"Schnitzel")
myList.insert(4,"Fish")
myList.insert(5,"Lasagne")
myList.config(height=myList.size())
entryBox = Entry(window)
entryBox.pack()
submitButton = Button(window,text="Submit",command=submit)
submitButton.pack()
addButton = Button(window,text="Add",command=addItem)
addButton.pack()
# Add a button to run a "deleteItem" function that removes selected item
deleteButton = Button(window,text="Delete",command=deleteItem)
deleteButton.pack()
window.mainloop()

Finally, what if you want to select more than one item from the listbox? We need to use an option called selectmode.
from tkinter import *
def submit():
yourSelection = [] # Create a LIST (array) called "yourSelection"
for index in myList.curselection(): # We need a loop to read data into the LIST
yourSelection.insert(index,myList.get(index))
print("You have ordered: ")
for index in yourSelection: # We need a loop to write the LIST data to screen
print(index)
def addItem():
myList.insert(myList.size(),entryBox.get())
# myList.size gets the next list number
myList.config(height=myList.size()) # will dynamically reset the height
def deleteItem():
for index in reversed(myList.curselection()): # Loop to delete MULTIPLE selections
myList.delete(index)
myList.config(height=myList.size()) # will dynamically reset the height
window = Tk()
myList = Listbox(window,
bg="#f7ffde",
font=("Constantia",35),
width=12,
selectmode=MULTIPLE # Allows MULTIPLE item selection
)
myList.pack()
myList.insert(1,"Pizza")
myList.insert(2,"Pasta")
myList.insert(3,"Schnitzel")
myList.insert(4,"Fish")
myList.insert(5,"Lasagne")
myList.config(height=myList.size())
entryBox = Entry(window)
entryBox.pack()
submitButton = Button(window,text="Submit",command=submit)
submitButton.pack()
addButton = Button(window,text="Add",command=addItem)
addButton.pack()
deleteButton = Button(window,text="Delete",command=deleteItem)
deleteButton.pack()
window.mainloop()
Paste the above code into your IDE and see it working.
A final example to demonstrate more styling settings:
from tkinter import *
def get_selected_item():
# Get the index of the selected item(s)
selection_indices = listbox.curselection()
if selection_indices:
# Get the actual value using the index
selected_item = listbox.get(selection_indices[0])
print(f"Selected item: {selected_item}")
else:
print("No item selected.")
root = Tk()
root.title("Tkinter Listbox Example")
# Create a frame to hold the listbox and scrollbar
frame = Frame(root)
frame.pack(padx=10, pady=10, fill=BOTH, expand=True)
# Create a Scrollbar
scrollbar = Scrollbar(frame, orient=VERTICAL)
# Create the Listbox and link it to the scrollbar
listbox = Listbox(frame, yscrollcommand=scrollbar.set, selectmode=SINGLE)
scrollbar.config(command=listbox.yview)
# Pack the scrollbar and listbox
scrollbar.pack(side=RIGHT, fill=Y)
listbox.pack(side=LEFT, fill=BOTH, expand=True)
# Add items to the listbox
items = ["Python", "Perl", "C", "PHP", "JSP", "Ruby", "Java", "C++"]
for item in items:
listbox.insert(END, item) # Insert at the end
# Create a button to get the selected item
button = Button(root, text="Get Selected Item", command=get_selected_item)
button.pack(pady=10)
root.mainloop()