Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
If you want to allow users to make multiple selections in Tkinter, the Listbox
widget with the MULTIPLE
selection mode is what you need.
Here's a tutorial on how to create a multiple selection interface using Tkinter:
Listbox
in TkinterSetting up the basic GUI with Tkinter:
Initialize a basic window.
import tkinter as tk root = tk.Tk() root.title("Multiple Selection with Tkinter") root.geometry("300x300")
Creating and Configuring the Listbox:
Use the Listbox
widget with selectmode=tk.MULTIPLE
for multiple selections.
# Create the listbox and populate it with options listbox = tk.Listbox(root, selectmode=tk.MULTIPLE) options = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"] for option in options: listbox.insert(tk.END, option) listbox.pack(pady=20)
Function to Retrieve Selected Items:
def get_selections(): selected_indices = listbox.curselection() selected_items = [listbox.get(i) for i in selected_indices] print("Selected Items:", selected_items)
The curselection
method returns a tuple of indices of the selected items. We then retrieve the selected items using these indices.
Add Button to Display Selections:
btn = tk.Button(root, text="Get Selections", command=get_selections) btn.pack(pady=20)
Run the main loop:
root.mainloop()
Now, put all the code snippets together and run the script. You'll see a Listbox
with multiple options. You can select multiple items by holding down the Ctrl key (Cmd key on macOS) and clicking on the desired options. Click the "Get Selections" button to print the selected items to the console.
This approach allows you to create a basic multiple selection interface. You can extend this functionality by adding more features or integrating with other parts of a larger Tkinter application.
Python Tkinter checkbox selection example:
Checkbutton
to create a checkbox for user selection.import tkinter as tk def on_checkbox_click(): selected_value.set("Checkbox is " + ("checked" if checkbox_var.get() else "unchecked")) root = tk.Tk() root.title("Tkinter Checkbox Selection Example") # Create a checkbox checkbox_var = tk.BooleanVar() checkbox = tk.Checkbutton(root, text="Check me", variable=checkbox_var, command=on_checkbox_click) checkbox.pack(pady=10) selected_value = tk.StringVar() label = tk.Label(root, textvariable=selected_value) label.pack(pady=10) root.mainloop()
Tkinter listbox multiple selection:
Listbox
widget.import tkinter as tk def on_selection(event): selected_items = listbox.curselection() selected_values.set([listbox.get(index) for index in selected_items]) root = tk.Tk() root.title("Tkinter Listbox Multiple Selection") # Create a Listbox with multiple selection listbox = tk.Listbox(root, selectmode=tk.MULTIPLE) listbox.pack(pady=10) # Add items to the Listbox items = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"] for item in items: listbox.insert(tk.END, item) # Bind selection event listbox.bind("<<ListboxSelect>>", on_selection) selected_values = tk.StringVar() label = tk.Label(root, textvariable=selected_values) label.pack(pady=10) root.mainloop()
How to create a multiselect dropdown in Tkinter:
Listbox
widget with multiple selection.import tkinter as tk def on_dropdown_selection(): selected_items = listbox.curselection() selected_values.set([listbox.get(index) for index in selected_items]) root = tk.Tk() root.title("Tkinter Multiselect Dropdown") # Create a dropdown with multiple selection listbox = tk.Listbox(root, selectmode=tk.MULTIPLE) listbox.pack(pady=10) # Add items to the dropdown items = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"] for item in items: listbox.insert(tk.END, item) # Bind selection event listbox.bind("<<ListboxSelect>>", lambda event: on_dropdown_selection()) selected_values = tk.StringVar() label = tk.Label(root, textvariable=selected_values) label.pack(pady=10) root.mainloop()
Tkinter multiple choice options:
Checkbutton
widgets.import tkinter as tk def on_checkbox_click(checkbox_var, label_text): selected_value.set("Selected: " + label_text if checkbox_var.get() else "") root = tk.Tk() root.title("Tkinter Multiple Choice Options") # Create multiple choice options using Checkbuttons options = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"] selected_value = tk.StringVar() for option in options: checkbox_var = tk.BooleanVar() checkbox = tk.Checkbutton(root, text=option, variable=checkbox_var, command=lambda var=checkbox_var, text=option: on_checkbox_click(var, text)) checkbox.pack() label = tk.Label(root, textvariable=selected_value) label.pack(pady=10) root.mainloop()
Checkbutton group in Tkinter:
Checkbutton
widgets in Tkinter using a shared variable.import tkinter as tk def on_checkbox_click(checkbox_var, label_text): selected_value.set("Selected: " + label_text if checkbox_var.get() else "") root = tk.Tk() root.title("Tkinter Checkbutton Group") # Group Checkbuttons using a shared variable checkbox_var = tk.BooleanVar() options = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"] selected_value = tk.StringVar() for option in options: checkbox = tk.Checkbutton(root, text=option, variable=checkbox_var, command=lambda text=option: on_checkbox_click(checkbox_var, text)) checkbox.pack() label = tk.Label(root, textvariable=selected_value) label.pack(pady=10) root.mainloop()