Enable multiple selection of values from a Tkinter Combobox


Tkinter is a powerful and popular GUI toolkit that is widely used for creating desktop applications in Python. One of the key components of any GUI application is the ability to select values from a list. Tkinter provides various widgets for this purpose, including the combobox, which is a combination of a text field and a dropdown list.

By default, the combobox in Tkinter allows only a single selection of value from the dropdown list. However, there may be situations where the user needs to select multiple values from the list. This can be achieved in Tkinter by enabling the multiple selection feature of the combobox.

In this article, we will discuss two approaches to achieve multiple selection of values from a tkinter combobox.

Using the Listbox Widget

The simplest way to enable multiple selection in a tkinter combobox is to use the Listbox widget. In this approach, we create a Listbox widget and add it to the combobox. The Listbox widget will display the dropdown list, and the user can select multiple values from it by clicking on the values.

Example

Here's an example code that demonstrates this approach −

import tkinter as tk
from tkinter import ttk

# create a tkinter window
window = tk.Tk()
window.geometry("720x250")
window.title("Multiple Selection Combobox using the Listbox widget")

# define the values for the dropdown list
values = ["Value 1", "Value 2", "Value 3", "Value 4", "Value 5"]

# create a label for the combobox
label = ttk.Label(window, text="Select values:")

# create a combobox
combobox = ttk.Combobox(window, state="readonly")

# create a Listbox widget for the dropdown list
listbox = tk.Listbox(window, selectmode="multiple", exportselection=0)
for value in values:
   listbox.insert(tk.END, value)

# define a function to update the combobox when the user selects or deselects a value
def update_combobox():
   # Get selected values from the Listbox widget
   selected_values = [listbox.get(idx) for idx in listbox.curselection()]
    
   # Update the combobox with the selected values
   combobox.configure(width=40, height=7)
   combobox.set(", ".join(selected_values))
    
# bind the update_combobox function to the Listbox widget
listbox.bind("<<ListboxSelect>>", lambda _: update_combobox())

# pack the label, combobox, and Listbox widget
label.pack(side="top", anchor="w", pady=30)
combobox.pack(side="top", pady=30)
listbox.pack(side="top")

# start the main loop
window.mainloop()

Output

In the above implementation, we created a tkinter window, a label for the combobox, and a combobox. We define the values for the dropdown list and create a Listbox widget to display the values.

We set the selectmode attribute of the Listbox widget to "multiple" to allow the user to select multiple values. We insert the values into the Listbox widget using the insert() method.

We also set the exportselection attribute of the Listbox widget to 0 to prevent it from deselecting previously selected items when the user clicks outside the Listbox.

To display the selected values in the combobox, we define a function update_combobox() that retrieves the selected values from the Listbox widget and updates the text field of the combobox accordingly. This function is called when the user selects or deselects a value in the Listbox widget.

We bind the <<ListboxSelect>> event to an anonymous function that calls the update_combobox() function to ensure that the combobox is updated whenever the user makes a selection.

When the user selects multiple values from the dropdown list, they will be displayed in the text field of the combobox separated by commas.

Using the Checkbutton Widget

Another approach to enable multiple selection in a tkinter combobox is to use the Checkbutton widget. In this approach, we create a Checkbutton widget for each value in the dropdown list and add them to the combobox. The user can select multiple values by clicking on the corresponding Checkbuttons.

Example

Here's an example code that demonstrates this approach −

import tkinter as tk
from tkinter import ttk

# create a tkinter window
window = tk.Tk()
window.geometry("720x250")
window.title("Multiple Selection Combobox using the Checkbox Widget")

# define the values for the dropdown list
values = ["Value 1", "Value 2", "Value 3", "Value 4", "Value 5"]

# create a label for the combobox
label = ttk.Label(window, text="Select values:")

# create a combobox
combobox = ttk.Combobox(window)

# create a list of BooleanVar objects to hold the state of the Checkbuttons
checkbuttons_vars = [tk.BooleanVar() for value in values]

# create a Checkbutton widget for each value in the dropdown list
checkbuttons = []
for index, value in enumerate(values):
   checkbutton = ttk.Checkbutton(window, text=value, variable=checkbuttons_vars[index])
   checkbutton.pack(side="top", anchor="w")
   checkbuttons.append(checkbutton)

# define a function to update the combobox when the user selects or deselects a value
def update_combobox():
   selected_values = [value for value, var in zip(values, checkbuttons_vars) if var.get()]
   combobox.configure(width=40, height=7)
   combobox.delete(0, tk.END)
   combobox.insert(0, ", ".join(selected_values))

# add a button to update the combobox
update_button = ttk.Button(window, text="Update", command=update_combobox)
update_button.pack(side="bottom")

# pack the label and combobox
label.pack(side="top", anchor="w", pady=30)
combobox.pack(side="top")

# start the main loop
window.mainloop()

Output

Overall, this approach provides a more intuitive user interface for selecting multiple values from a tkinter combobox, but requires more coding to create the Checkbuttons and update the combobox.

Conclusion

In conclusion, enabling multiple selection of values from a tkinter combobox can be achieved in several ways. We have discussed two approaches in this article: using the Listbox widget and using the Checkbutton widget.

When using the Listbox widget, the user can select multiple values by clicking on the values. This approach is simple and requires minimal coding but may not be as intuitive for the user.

When using the Checkbutton widget, the user can select multiple values by clicking on the corresponding Checkbuttons. This approach provides a more intuitive user interface but requires more coding to create the Checkbuttons and update the combobox.

Overall, enabling multiple selection in a tkinter combobox is a useful feature that can enhance the functionality and usability of GUI applications.

Updated on: 04-Dec-2023

447 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements