How can I create a dropdown menu from a List in Tkinter?

Creating a dropdown menu in Tkinter is simple using the OptionMenu widget. This widget allows users to select from a predefined list of options through a dropdown interface.

Basic Dropdown Menu

To create a dropdown menu, use OptionMenu(parent, variable, *values) where the variable stores the selected value −

from tkinter import *

# Create main window
win = Tk()
win.geometry("400x200")
win.title("Dropdown Menu Example")

# Create StringVar to store selected value
selected_language = StringVar()
selected_language.set("Select Any Language")

# Create dropdown menu
languages = ["C++", "Java", "Python", "JavaScript", "Rust", "GoLang"]
dropdown = OptionMenu(win, selected_language, *languages)
dropdown.pack(pady=20)

win.mainloop()

Getting Selected Value

You can retrieve the selected value using the get() method of StringVar and respond to selection changes −

from tkinter import *

def on_select():
    selected = selected_language.get()
    result_label.config(text=f"You selected: {selected}")

win = Tk()
win.geometry("400x250")
win.title("Interactive Dropdown")

# Create StringVar and set default
selected_language = StringVar()
selected_language.set("Choose Language")

# Create dropdown
languages = ["Python", "JavaScript", "Java", "C++", "Go"]
dropdown = OptionMenu(win, selected_language, *languages)
dropdown.pack(pady=20)

# Button to get selection
select_btn = Button(win, text="Get Selection", command=on_select)
select_btn.pack(pady=10)

# Label to show result
result_label = Label(win, text="No selection made", font=("Arial", 12))
result_label.pack(pady=10)

win.mainloop()

Automatic Selection Handling

You can bind a function to execute automatically when the selection changes using the trace method −

from tkinter import *

def selection_changed(*args):
    selected = selected_language.get()
    if selected != "Select Language":
        status_label.config(text=f"Current: {selected}", fg="green")

win = Tk()
win.geometry("400x200")
win.title("Auto-Update Dropdown")

# Create StringVar with trace
selected_language = StringVar()
selected_language.set("Select Language")
selected_language.trace('w', selection_changed)

# Create dropdown
languages = ["Python", "Java", "C++", "JavaScript", "Ruby"]
dropdown = OptionMenu(win, selected_language, *languages)
dropdown.pack(pady=20)

# Status label
status_label = Label(win, text="Make a selection", font=("Arial", 10))
status_label.pack(pady=10)

win.mainloop()

Key Points

  • StringVar() stores the selected value from the dropdown
  • OptionMenu() accepts parent window, variable, and option values
  • Use set() to define the default selection
  • Use get() to retrieve the current selection
  • Use trace() for automatic response to selection changes

Conclusion

OptionMenu provides an easy way to create dropdown menus in Tkinter. Combine it with StringVar for value storage and event handling for interactive applications.

Updated on: 2026-03-25T18:30:03+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements