~/Tkinter Dropdown Menu Quick Guide

Jul 15, 2023


To create a dropdown menu in Tkinter, use the OptionMenu widget. This widget lets users choose one option from a short list.

Example code for a dropdown menu:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import tkinter as tk

root = tk.Tk()

var = tk.StringVar(root)
var.set("Option 1")

dropdown = tk.OptionMenu(root, var, "Option 1", "Option 2", "Option 3")
dropdown.pack()

root.mainloop()

Change the default value by setting var.set("your option"). Extract the selection using var.get().

For customizing appearance or more complex dropdowns, use ttk Combobox:

1
2
3
4
5
6
from tkinter import ttk

combo = ttk.Combobox(root, values=["Option 1", "Option 2", "Option 3"])
combo.pack()
combo.current(0)
print(combo.get())

Both widgets work with Python 3 and standard Tkinter installations.

Tags: [tkinter] [python] [gui]