Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Enable multiple selection of values from a Tkinter Combobox
Tkinter is a powerful GUI toolkit for creating desktop applications in Python. The Combobox widget normally allows only single selection, but there are scenarios where multiple selection is needed. This article explores two approaches to enable multiple selection functionality.
Using the Listbox Widget
The simplest approach is to combine a Combobox with a Listbox widget. The Listbox displays selectable options, while the Combobox shows the selected values.
Example
Here's a complete implementation using the Listbox approach ?
import tkinter as tk
from tkinter import ttk
# Create main window
window = tk.Tk()
window.geometry("400x350")
window.title("Multiple Selection with Listbox")
# Define dropdown values
values = ["Python", "Java", "C++", "JavaScript", "Go"]
# Create widgets
label = ttk.Label(window, text="Select Programming Languages:")
combobox = ttk.Combobox(window, state="readonly", width=30)
listbox = tk.Listbox(window, selectmode="multiple", exportselection=0, height=6)
# Populate listbox
for value in values:
listbox.insert(tk.END, value)
# Function to update combobox display
def update_combobox(event=None):
selected_indices = listbox.curselection()
selected_values = [listbox.get(idx) for idx in selected_indices]
combobox.set(", ".join(selected_values))
# Bind selection event
listbox.bind("<<ListboxSelect>>", update_combobox)
# Layout widgets
label.pack(pady=10, anchor="w", padx=20)
combobox.pack(pady=5, padx=20, fill="x")
tk.Label(window, text="Available Options:").pack(pady=(20,5), anchor="w", padx=20)
listbox.pack(pady=5, padx=20, fill="both", expand=True)
window.mainloop()
In this implementation, we set selectmode="multiple" to allow multiple selections and exportselection=0 to prevent automatic deselection. The selected values are displayed in the combobox as a comma-separated string.
Using Checkbutton Widgets
This approach creates individual checkboxes for each option, providing a more visual interface for multiple selections.
Example
Here's the implementation using Checkbuttons ?
import tkinter as tk
from tkinter import ttk
# Create main window
window = tk.Tk()
window.geometry("400x350")
window.title("Multiple Selection with Checkbuttons")
# Define dropdown values
values = ["Python", "Java", "C++", "JavaScript", "Go"]
# Create widgets
label = ttk.Label(window, text="Select Programming Languages:")
combobox = ttk.Combobox(window, state="readonly", width=30)
# Create BooleanVar objects for checkbuttons
checkbox_vars = [tk.BooleanVar() for _ in values]
# Function to update combobox
def update_combobox():
selected_values = [value for value, var in zip(values, checkbox_vars) if var.get()]
combobox.set(", ".join(selected_values))
# Create checkbuttons
checkbox_frame = ttk.Frame(window)
for i, value in enumerate(values):
checkbox = ttk.Checkbutton(
checkbox_frame,
text=value,
variable=checkbox_vars[i],
command=update_combobox
)
checkbox.pack(anchor="w", pady=2)
# Layout widgets
label.pack(pady=10, anchor="w", padx=20)
combobox.pack(pady=5, padx=20, fill="x")
tk.Label(window, text="Available Options:").pack(pady=(20,5), anchor="w", padx=20)
checkbox_frame.pack(pady=5, padx=20, fill="both")
window.mainloop()
This approach uses BooleanVar objects to track checkbox states and automatically updates the combobox whenever a checkbox is toggled using the command parameter.
Comparison
| Method | User Interface | Code Complexity | Best For |
|---|---|---|---|
| Listbox | Compact, scrollable | Simple | Many options, limited space |
| Checkbuttons | Individual checkboxes | Moderate | Few options, clear visual feedback |
Conclusion
Both approaches effectively enable multiple selection in Tkinter Combobox widgets. Use the Listbox method for compact interfaces with many options, or Checkbuttons for intuitive visual selection with fewer choices.
