In this tutorial, I will explain how to create and customize listboxes in Python Tkinter. I recently worked on a project where I needed to display a list of US states and allow users to select multiple states from the list. That’s when I discovered the power and flexibility of the Tkinter Listbox widget. In this article, I’ll share my experience and guide you through the process of creating and customizing listboxes in Python Tkinter.
Create Listbox in Python Tkinter
To create a basic listbox in Tkinter, you can use the Listbox widget. Here’s an example of how to create a simple list box:
from tkinter import *
root = Tk()
root.title("US States Listbox")
states = ["California", "New York", "Texas", "Florida", "Illinois"]
listbox = Listbox(root)
for state in states:
listbox.insert(END, state)
listbox.pack()
root.mainloop()I have executed the above example code and added the screenshot below.

In this example, we create a window titled “US States Listbox” using Tk(). We define a list of states that contains the names of some US states. Then, we create a Listbox widget and use a for loop to insert each state from the states list into the list box using the insert() method. Finally, we pack the listbox into the window and start the main event loop with mainloop().
Read Python Tkinter Validation examples
Customize Listbox in Python Tkinter
Tkinter provides various options to customize the appearance of listboxes. Here are a few common customization options:
1. Set Listbox Dimensions
You can set the width and height of the list box using the width and height parameters, respectively. For example:
listbox = Listbox(root, width=20, height=10)This creates a list box with a width of 20 characters and a height of 10 lines.
Read Python QR code generator using pyqrcode in Tkinter
2. Change Listbox Colors
You can change the background and foreground colors of the list box using the bg and fg parameters, respectively. For example:
listbox = Listbox(root, bg="lightblue", fg="darkblue")I have executed the above example code and added the screenshot below.

This sets the background color to light blue and the text color to dark blue.
3. Style Listbox Items
You can apply different styles to listbox items using the selectmode parameter. The available options are:
SINGLE: Allows selecting only one item at a time (default).BROWSE: Allows selecting one item at a time, and the selection follows the mouse cursor.MULTIPLE: Allows selecting multiple items using Ctrl/Shift keys.EXTENDED: Allows selecting multiple items using Ctrl/Shift keys or by dragging the mouse.
For example:
listbox = Listbox(root, selectmode=MULTIPLE)I have executed the above example code and added the screenshot below.

This allows users to select multiple items in the list box using Ctrl/Shift keys.
Read Python Tkinter On-Off Switch
Handle Listbox Events
Tkinter provides event-handling mechanisms to respond to user interactions with the list box. Here are a few common events and how to handle them:
1. Detect Item Selection
You can detect when an item is selected in the list box using the <<ListboxSelect>> event. Here’s an example:
def on_select(event):
selected_indices = listbox.curselection()
selected_items = [listbox.get(i) for i in selected_indices]
print("Selected items:", selected_items)
listbox.bind("<<ListboxSelect>>", on_select)I have executed the above example code and added the screenshot below.

In this example, we define a function on_select that is triggered whenever an item is selected in the list box. We retrieve the selected indices curselection() and then get the corresponding items using get(). Finally, we print the selected items.
2. Retrieve Selected Items
To retrieve the selected items from the list box, you can use the curselection() method, which returns a tuple of indices of the selected items. You can then use the get() method to retrieve the actual item values. For example:
selected_indices = listbox.curselection()
selected_items = [listbox.get(i) for i in selected_indices]I have executed the above example code and added the screenshot below.

This retrieves the indices of the selected items and then uses a list comprehension to get the corresponding item values.
Read Python Tkinter notebook Widget
Python Tkinter Listbox set item
Python set selected item refers to setting the default item in the Python Tkinter Listbox. Whenever the user runs the program a default value is already selected
from tkinter import *
# Create the main window
ws = Tk()
ws.title('USA Guide')
ws.geometry('400x300')
# Function to display the selected item
def showSelected(event):
show.config(text=lb.get(ANCHOR))
# Create the Listbox
lb = Listbox(ws,selectmode=MULTIPLE, bg="lightblue", fg="darkblue")
lb.pack()
# Insert USA-specific items into the Listbox
usa_items = [
"California", "New York", "Texas", "Florida", "Illinois",
"Chicago", "Los Angeles", "San Francisco", "Washington D.C.", "Boston"
]
for item in usa_items:
lb.insert(END, item)
# Set the default selection (for example, selecting "Texas")
lb.select_set(2) # This will select the third item, which is "Texas"
# Bind the Listbox select event to update the label when an item is selected
lb.bind("<<ListboxSelect>>", showSelected)
# Label to display the selected item
show = Label(ws)
show.pack(pady=10)
# Run the main event loop
ws.mainloop()I have executed the above example code and added the screenshot below.

In this output, Texas is selected by default immediately after running the program.
Python Tkinter Listbox scrollbar
Let us check how to implement a scrollbar in Python Tkinter Listbox.
Scrollbars are used when the content to be displayed is more than a dedicated area. It facilitates putting a large amount of data on the screen. This data can be accessed by scrolling the scrollbar. The scrollbar can be vertical or horizontal.
To implement a scrollbar in Python Tkinter Listbox there are mainly 3 steps:
- create a scrollbar and put it in a parent or frame window
- set the type of scrollbar: Horizontal or Vertical.
- configure the scrollbar.
Vertical Scrollbar:
from tkinter import *
# Create the main window
root = Tk()
root.title("USA Guide")
root.geometry("400x300")
# List of USA-specific items (states and cities)
usa_items = [
"California", "New York", "Texas", "Florida", "Illinois",
"Chicago", "Los Angeles", "San Francisco", "Washington D.C.", "Boston",
"Seattle", "Dallas", "Atlanta", "Denver", "Miami", "Las Vegas"
]
# Create the frame to hold the Listbox and Scrollbar
frame = Frame(root)
frame.pack(pady=10)
# Create the Listbox with multiple selection enabled
listbox = Listbox(frame, selectmode=MULTIPLE, bg="lightblue", fg="darkblue")
listbox.pack(side=LEFT, fill=BOTH, expand=True)
# Create the vertical Scrollbar
scrollbar = Scrollbar(frame, orient=VERTICAL)
scrollbar.pack(side=RIGHT, fill=Y)
# Configure the Listbox to use the Scrollbar
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
# Insert USA-specific items into the Listbox
for item in usa_items:
listbox.insert(END, item)
# Function to display the selected items
def showSelected():
selected_indices = listbox.curselection()
selected_items = [listbox.get(i) for i in selected_indices]
show.config(text=f"Selected: {', '.join(selected_items)}")
# Bind selection event to dynamically display selected items
def on_select(event):
showSelected()
listbox.bind("<<ListboxSelect>>", on_select)
# Create a Label to display selected items
show = Label(root, text="Selected: None", bg="#446644", fg="white", wraplength=350)
show.pack(pady=10)
# Run the main event loop
root.mainloop()I have executed the above example code and added the screenshot below.

Horizontal Scrollbar:
from tkinter import *
# Create the main window
root = Tk()
root.title("USA Guide")
root.geometry("400x300")
# List of USA-specific items (states and cities)
usa_items = [
"California", "New York", "Texas", "Florida", "Illinois",
"Chicago", "Los Angeles", "San Francisco", "Washington D.C.", "Boston"
]
# Create a frame to hold the Listbox and Scrollbar
frame = Frame(root)
frame.pack(pady=10)
# Create the Listbox with multiple selection enabled
listbox = Listbox(frame, selectmode=MULTIPLE, bg="lightblue", fg="darkblue", width=25)
listbox.pack(side=LEFT)
# Create a horizontal Scrollbar for the Listbox
scrollbar = Scrollbar(frame, orient=HORIZONTAL, command=listbox.xview)
scrollbar.pack(fill=X)
# Configure the Listbox to use the horizontal Scrollbar
listbox.config(xscrollcommand=scrollbar.set)
# Insert USA-specific items into the Listbox
for item in usa_items:
listbox.insert(END, item)
# Function to display the selected items
def showSelected():
selected_indices = listbox.curselection()
selected_items = [listbox.get(i) for i in selected_indices]
show.config(text=f"Selected: {', '.join(selected_items)}")
# Bind selection event to dynamically display selected items
def on_select(event):
showSelected()
listbox.bind("<<ListboxSelect>>", on_select)
# Create a Label to display selected items
show = Label(root, text="Selected: None", bg="#446644", fg="white", wraplength=350)
show.pack(pady=10)
# Run the main event loop
root.mainloop()Python Tkinter Listbox with checkboxes
- There’s no way to put the checkboxes inside the Listbox.
- There used to be a
tixchecklistwidget using which a checkbox could be created inside the Listbox. - But a tixchecklist was discontinued after Python 3.6.
Python Tkinter Listbox bind
Python Listbox bind is used to set an action that will occur when the event is performed.Any activity that happens on the Listbox widget is called an event. focus-gained, focus-lost, clicked, etc are a few examples of events. In this section, we will learn about a few popular bind events:
<<ListboxSelect>>: It tells about when the user selected or deselected Listbox item(s)<Double-1>: It triggers activity when the user double-clicks on an item(s) in a Listbox.
from tkinter import *
def showSelected(event):
temp= str(event) + '\n' + str(lb.curselection())
show.configure(text=temp)
ws = Tk()
ws.title('Python Guides')
ws.geometry('400x250')
ws.config(bg='#F9F833')
var = StringVar(value=dir())
lb = Listbox(ws, listvariable=var, selectmode='extended')
lb.pack()
lb.bind('<<ListboxSelect>>', showSelected)
show = Label(ws)
show.pack()
ws.mainloop()Label shows the index number(s) of selected item(s) in Listbox.
Read Python Tkinter Multiple Windows Tutorial
Example: US State Selection Application
Let’s put everything together and create a sample application that allows users to select US states from a list box and display the selected states in a label. Here’s the code:
from tkinter import *
def on_select(event):
selected_indices = listbox.curselection()
selected_states = [listbox.get(i) for i in selected_indices]
label.config(text="Selected States: " + ", ".join(selected_states))
root = Tk()
root.title("US State Selection")
states = ["Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "Florida", "Georgia",
"Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",
"Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
"Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri",
"Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey",
"New Mexico", "New York", "North Carolina", "North Dakota", "Ohio",
"Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
"Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"]
listbox = Listbox(root, selectmode=MULTIPLE, width=20, height=10)
for state in states:
listbox.insert(END, state)
listbox.pack()
listbox.bind("<<ListboxSelect>>", on_select)
label = Label(root, text="Selected States:")
label.pack()
root.mainloop()In this example, we create a list box with all the US states and allow multiple selections using selectmode=MULTIPLE. We bind the <<ListboxSelect>> event to the on_select function, which retrieves the selected states and updates the label text accordingly.
Read Python Tkinter Editor + Examples
Conclusion
In this tutorial, I explained how to create and customize listboxes in Python Tkinter. I covered some list box creating and customizing methods, and how to handle list box events, We also discussed how to set items in Python Tkinter, list box scrollbar, list box with check box, list box bind, and an example.
You may also like to read:
- Python Tkinter Editor + Examples
- Python Tkinter Table Tutorial
- Python Tkinter Quiz – Complete tutorial

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.