How do I use an OptionMenu in tkinter?
An OptionMenu is built around a variable rather than returning a value of its own. You pass the variable in, the options after it, and read the choice back from that same variable.
import tkinter as tk
root = tk.Tk()
choice = tk.StringVar(value="medium") # also the label on the button
tk.OptionMenu(root, choice, "small", "medium", "large").pack()
def go():
print("picked", choice.get())
tk.Button(root, text="Go", command=go).pack()
root.mainloop() Why the button comes up blank
The button shows whatever the variable holds, and a fresh StringVar holds the empty string — so an OptionMenu with a bare variable is a blank button until someone opens it and picks something. Give the variable a starting value, and it doubles as the default choice.
choice = tk.StringVar(value="medium") # not tk.StringVar()Changing the options at runtime
The options are baked in at construction, so there is no values option to reassign as there is on a Combobox. What you can do is reach the underlying menu, empty it, and add fresh entries — each one setting the variable when chosen.
This works, but it is fiddly enough that a ttk.Combobox is usually the better widget when the list changes: there, replacing the choices is a single assignment.
menu = option_menu["menu"]
menu.delete(0, "end")
for opt in ("alpha", "beta"):
menu.add_command(label=opt, command=lambda o=opt: choice.set(o))Reacting to a choice
Rather than a command on the widget, put a trace on the variable — it fires whenever the value changes, including when your own code sets it. The callback receives three positional arguments from Tk that you can usually ignore.
def on_change(*_):
print("now", choice.get())
choice.trace_add("write", on_change) Doing it without the lookup
Type the options one per line and the export writes the StringVar, the OptionMenu and the default for you. If you expect the list to change while the app runs, choose a Combobox in the designer instead — the same inspector field feeds both.
Lay the window out visually and read the generated Python as you go.
Open the tkinter designer →