Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
The Listbox
widget in tkinter
is a standard widget used for displaying a list of strings to a user. Users can then make selections from the list.
tkinter
Tutorial:1. Import Required Libraries:
import tkinter as tk from tkinter import messagebox
2. Initialize Main Application Window:
root = tk.Tk() root.title("ListBox Widget Tutorial")
3. Creating a Basic Listbox:
listbox = tk.Listbox(root) listbox.pack(pady=15)
4. Add Items to the Listbox:
items = ["Apple", "Banana", "Cherry", "Date", "Fig", "Grape", "Kiwi"] for item in items: listbox.insert(tk.END, item)
5. Function to Display the Selected Item:
def display_selection(): try: selected_item = listbox.get(listbox.curselection()) messagebox.showinfo("Selection", f"You selected: {selected_item}") except: messagebox.showwarning("Selection", "No item selected") btn = tk.Button(root, text="Show Selection", command=display_selection) btn.pack(pady=15)
6. Some Commonly Used Options with Listbox:
selectmode
: Determines how many items can be selected. Values include tk.SINGLE
(default, only one item), tk.BROWSE
, tk.MULTIPLE
(multiple items), and tk.EXTENDED
(multiple items using Shift and Ctrl).height
: The number of lines (items) shown in the listbox.bg
: Background color.fg
: Foreground color (color of the text).font
: Defines the font type and size.Example with some options:
listbox_custom = tk.Listbox(root, bg="lightgray", fg="blue", height=4, selectmode=tk.MULTIPLE, font=('Arial', 12)) listbox_custom.pack(pady=15) for item in items: listbox_custom.insert(tk.END, item)
7. Scrolling with Listbox:
A Scrollbar
can be associated with a Listbox
to make it scrollable when there are more items than can be displayed.
scrollbar = tk.Scrollbar(root, orient=tk.VERTICAL) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) scrollable_listbox = tk.Listbox(root, yscrollcommand=scrollbar.set) scrollable_listbox.pack(pady=15) for item in items * 3: # Replicating items to fill up the list scrollable_listbox.insert(tk.END, item) scrollbar.config(command=scrollable_listbox.yview)
8. Mainloop to Run the Application:
root.mainloop()
Complete Code:
Putting it all together:
import tkinter as tk from tkinter import messagebox def display_selection(): try: selected_item = listbox.get(listbox.curselection()) messagebox.showinfo("Selection", f"You selected: {selected_item}") except: messagebox.showwarning("Selection", "No item selected") root = tk.Tk() root.title("ListBox Widget Tutorial") listbox = tk.Listbox(root) listbox.pack(pady=15) items = ["Apple", "Banana", "Cherry", "Date", "Fig", "Grape", "Kiwi"] for item in items: listbox.insert(tk.END, item) btn = tk.Button(root, text="Show Selection", command=display_selection) btn.pack(pady=15) listbox_custom = tk.Listbox(root, bg="lightgray", fg="blue", height=4, selectmode=tk.MULTIPLE, font=('Arial', 12)) listbox_custom.pack(pady=15) for item in items: listbox_custom.insert(tk.END, item) scrollbar = tk.Scrollbar(root, orient=tk.VERTICAL) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) scrollable_listbox = tk.Listbox(root, yscrollcommand=scrollbar.set) scrollable_listbox.pack(pady=15) for item in items * 3: scrollable_listbox.insert(tk.END, item) scrollbar.config(command=scrollable_listbox.yview) root.mainloop()
This code demonstrates the basic setup and functionality of a Listbox
widget in tkinter
. You can customize and extend this example to suit your application's needs.
Python Tkinter create ListBox example:
ListBox
widget in Tkinter is used to display a list of items.import tkinter as tk root = tk.Tk() # Create a ListBox listbox = tk.Listbox(root) listbox.pack(padx=10, pady=10) root.mainloop()
How to use ListBox widget in Tkinter:
ListBox
widget allows the user to select one or more items from a list.import tkinter as tk root = tk.Tk() # Create a ListBox listbox = tk.Listbox(root) listbox.pack(padx=10, pady=10) # Add items to the ListBox items = ["Item 1", "Item 2", "Item 3", "Item 4"] for item in items: listbox.insert(tk.END, item) root.mainloop()
Tkinter ListBox widget options and configuration:
ListBox
widget has various options for configuring its appearance, including options for font, background color, foreground color, etc.import tkinter as tk root = tk.Tk() # Customize the appearance of a ListBox listbox = tk.Listbox(root, bg="lightgray", fg="blue", font=("Arial", 12)) listbox.pack(padx=10, pady=10) root.mainloop()
Python Tkinter bind ListBox selection events:
<<ListboxSelect>>
event to handle selection events in a ListBox
.import tkinter as tk def on_select(event): selected_index = listbox.curselection() if selected_index: selected_item = listbox.get(selected_index[0]) print("Selected:", selected_item) root = tk.Tk() # Create a ListBox with selection binding listbox = tk.Listbox(root) listbox.pack(padx=10, pady=10) listbox.bind("<<ListboxSelect>>", on_select) items = ["Item 1", "Item 2", "Item 3", "Item 4"] for item in items: listbox.insert(tk.END, item) root.mainloop()
Handling multiple selections in Tkinter ListBox:
selectmode
option to allow multiple selections in a ListBox
.import tkinter as tk def on_select(event): selected_indices = listbox.curselection() selected_items = [listbox.get(index) for index in selected_indices] print("Selected:", selected_items) root = tk.Tk() # Create a ListBox with multiple selections listbox = tk.Listbox(root, selectmode=tk.MULTIPLE) listbox.pack(padx=10, pady=10) listbox.bind("<<ListboxSelect>>", on_select) items = ["Item 1", "Item 2", "Item 3", "Item 4"] for item in items: listbox.insert(tk.END, item) root.mainloop()
Tkinter ListBox widget grid and pack methods:
grid
or pack
method to place a ListBox
within a Tkinter window.import tkinter as tk root = tk.Tk() # Use both pack and grid to manage ListBox listbox = tk.Listbox(root) listbox.pack(side=tk.LEFT) listbox.grid(row=0, column=1) root.mainloop()