Creating a Multi-Select Drop-Down List in Tkinter

Tkinter provides a standard combobox widget for single-item selection, you can create a multi-select drop-down list using the Listbox widget along with buttons for adding and removing items.

Importing Tkinter

Before you can create a multi-select drop-down list, make sure to import the Tkinter library:

        
import tkinter as tk
        
    

Creating the Multi-Select Drop-Down List

To create the multi-select drop-down list, follow these steps:

        

root = tk.Tk()
root.title("Multi-Select Drop-Down List")

listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)
listbox.pack()

options = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"]

for option in options:
    listbox.insert(tk.END, option)
        
    

Adding and Removing Items

To allow users to add and remove items, create buttons and associated functions:

        

def add_item():
    selected_indices = listbox.curselection()
    for index in selected_indices:
        selected_item = listbox.get(index)
        if selected_item not in selected_items:
            selected_items.append(selected_item)
            selected_listbox.insert(tk.END, selected_item)

def remove_item():
    selected_indices = selected_listbox.curselection()
    for index in selected_indices:
        item_to_remove = selected_listbox.get(index)
        selected_items.remove(item_to_remove)
        selected_listbox.delete(index)

add_button = tk.Button(root, text="Add", command=add_item)
remove_button = tk.Button(root, text="Remove", command=remove_item)
add_button.pack()
remove_button.pack()
        
    

Displaying Selected Items

Selected items are displayed in a separate Listbox:

        

selected_items = []
selected_listbox = tk.Listbox(root)
selected_listbox.pack()
        
    

Running the Application

Finally, start the Tkinter main loop to run the application:

        
root.mainloop()
        
    
See also  Tkinter GUI to fetch data