Python > GUI Programming with Python > Tkinter > Common Widgets (Buttons, Labels, Entry Fields, Lists, etc.)
Tkinter Listbox Example
This snippet demonstrates the use of a Tkinter Listbox widget. It allows users to select items from a list. The selected item is then displayed in a label.
Complete Code
This code creates a Tkinter Listbox widget and populates it with a list of fruits. A double-click event is bound to the Listbox. When an item is double-clicked, the `on_double_click` function is called. This function retrieves the selected item and displays it in a Label.
import tkinter as tk
from tkinter import ttk
class ListboxApp:
def __init__(self, root):
self.root = root
self.root.title("Listbox Example")
# Listbox
self.listbox = tk.Listbox(root, width=30, height=10)
self.listbox.grid(row=0, column=0, padx=10, pady=10)
# Add items to the Listbox
items = ["Apple", "Banana", "Cherry", "Date", "Fig", "Grape"]
for item in items:
self.listbox.insert(tk.END, item)
# Bind double-click event to a function
self.listbox.bind('<Double-Button-1>', self.on_double_click)
# Result Label
self.result_label = ttk.Label(root, text="")
self.result_label.grid(row=1, column=0, pady=5)
def on_double_click(self, event):
# Get selected item
selected_indices = self.listbox.curselection()
if selected_indices:
selected_item = self.listbox.get(selected_indices[0])
self.result_label.config(text=f"You selected: {selected_item}")
else:
self.result_label.config(text="No item selected")
if __name__ == "__main__":
root = tk.Tk()
app = ListboxApp(root)
root.mainloop()
Concepts Behind the Snippet
This snippet illustrates:
tk.Listbox()
widget is used to display a list of items.insert()
method is used to add items to the Listbox. tk.END
specifies that the item should be added to the end of the list.bind()
method is used to bind a function to a specific event. In this case, the <Double-Button-1>
event (double-click with the left mouse button) is bound to the on_double_click
function.curselection()
method returns a tuple of indices of the selected items. The get()
method retrieves the item at a specific index.
Real-Life Use Case
Listbox widgets are commonly used in:
Best Practices
selectmode
option can be set to tk.MULTIPLE
or tk.EXTENDED
.
When to Use Them
Use Listbox widgets when:
Interview Tip
When asked about Tkinter, be prepared to discuss layout managers (grid, pack, place), event handling (binding functions to widgets), and common widgets like Label, Button, Entry, and Listbox. Knowing the difference between Tk and ttk widgets is also useful.
Alternatives
Alternatives to the Listbox widget include:
FAQ
-
How do I add a scrollbar to a Listbox?
You can add a scrollbar to a Listbox using the following code:scrollbar = tk.Scrollbar(root) scrollbar.grid(row=0, column=1, sticky='ns') listbox = tk.Listbox(root, yscrollcommand=scrollbar.set) listbox.grid(row=0, column=0, sticky='nsew') scrollbar.config(command=listbox.yview)
This code creates a Scrollbar widget and associates it with the Listbox using theyscrollcommand
option. -
How do I allow multiple selections in a Listbox?
You can allow multiple selections by setting theselectmode
option totk.MULTIPLE
ortk.EXTENDED
:listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)
tk.MULTIPLE
allows the user to select multiple items by clicking on them.tk.EXTENDED
allows the user to select a range of items by dragging the mouse or using the Shift key.