12. tk Optionmenu


12.1. Usage

The tkinter.OptionMenu widget provides a dropdown menu for selecting one option from a predefined list.
To create an OptionMenu widget, the general syntax is
(assuming import via "import tkinter as tk"):
option_menu_widget = tk.OptionMenu(parent, variable, *values)
parent is the window or frame object.
variable is a tk.StringVar that holds the currently selected value.
*values are the options to be displayed in the dropdown menu, passed as separate arguments.
The StringVar stores the currently selected option.
Whenever the user selects a different option from the dropdown menu, the value of the StringVar is updated automatically.
Unlike a Button, the OptionMenu automatically updates the associated StringVar whenever a different menu item is selected.
Likewise, calling set() on the StringVar changes the selected option shown in the OptionMenu.
The OptionMenu consists of a main button and a dropdown menu.
An OptionMenu is made up of two widgets:
  • the button that displays the current selection

  • the dropdown Menu containing the available options

Because these are separate widgets, they are configured separately.
Both can be configured using the config method:
option_menu_widget.config(option=value)
Allows you to set one or more option=value pairs to customize the widget.
To configure the dropdown menu itself, access the menu directly:
option_menu_widget["menu"].config(option=value)
Similarly, multiple option=value pairs can be passed to customize the menu's behavior or appearance.

12.2. Full constructor

The full constructor adds an optional command argument.
option_menu_widget = tk.OptionMenu(parent, variable, *values, command=my_function)
parent is the window or frame object.
variable is a tk.StringVar that holds the currently selected value.
*values are the options to be displayed in the dropdown menu, passed as separate arguments.
command is an optional function that is called whenever the selected option changes.
e.g. option_menu = tk.OptionMenu(root, variable, "Apple", "Banana", "Cherry", command=my_function)

Note

Although OptionMenu() accepts a command argument, it is not a configuration option of the widget. Therefore command does not appear in the output of option_menu_widget.keys().


12.3. Sample OptionMenu

../_images/option_menu.png
import tkinter as tk
from tkinter import font

# Create the main window
root = tk.Tk()
root.title("Fruit Selector")
root.geometry("300x200")

# Define a variable to hold the selected option
fruit_var = tk.StringVar(root)

# Define the options for the OptionMenu
fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry"]

# Set the default value for the OptionMenu
fruit_var.set(fruits[0])

# Define the font styles
font_style1 = ("Arial", 16, "bold")
font_style2 = ("Arial", 14)

# Create the OptionMenu widget
option_menu = tk.OptionMenu(root, fruit_var, *fruits)
option_menu.config(font=font_style1, bg="light green", fg="black", activebackground="dark green", activeforeground="white")
option_menu["menu"].config(font=font_style2, bg="light blue", fg="black", activebackground="dark blue", activeforeground="white")
option_menu.pack(pady=10, padx=10)

# Run the main event loop
root.mainloop()
*fruits: The asterisk (*) is used to unpack the list fruits into individual arguments.
This means that each item in the fruits list is passed as a separate argument to the OptionMenu.
For example, if fruits = ["Apple", "Banana", "Cherry"], the *fruits will be equivalent to passing "Apple", "Banana", "Cherry" as separate arguments.

Tasks

  1. Modify the code above to change the OptionMenu options to ["Mango", "Pineapple", "Grapes", "Orange", "Strawberry"]. The OptionMenu widget's background (bg) is set to "light yellow" and the text color (fg) is set to "blue". The active background (activebackground) is set to "orange" and the active text color (activeforeground) is set to "red". The menu's background (bg) is set to "light pink" and the text color (fg) is set to "purple". The active background (activebackground) is set to "yellow" and the active text color (activeforeground) is set to "green".

../_images/option_menu_question.png

Modify the code above to change the OptionMenu options.

import tkinter as tk
from tkinter import font

# Create the main window
root = tk.Tk()
root.title("Fruit Selector")
root.geometry("300x200")

# Define a variable to hold the selected option
fruit_var = tk.StringVar(root)

# Define the options for the OptionMenu
fruits = ["Mango", "Pineapple", "Grapes", "Orange", "Strawberry"]

# Set the default value for the OptionMenu
fruit_var.set(fruits[0])

# Define the font style
font_style1 = ("Arial", 16, "bold")
font_style2 = ("Arial", 14)

# Create the OptionMenu widget
option_menu = tk.OptionMenu(root, fruit_var, *fruits)
option_menu.config(font=font_style1, bg="light yellow", fg="blue", activebackground="orange", activeforeground="red")
option_menu["menu"].config(font=font_style2, bg="light pink", fg="purple", activebackground="yellow", activeforeground="green")
option_menu.pack(pady=10, padx=10)

# Run the main event loop
root.mainloop()

12.4. Methods

variable.get()
Returns the currently selected option.
variable.set(value)
Changes the selected option to value.
value should match one of the options supplied when the OptionMenu was created.

12.5. Parameter syntax

option_menu_widget = tk.OptionMenu(parent, variable, *values)
parent is the window or frame object where the OptionMenu will be placed.
variable is a Tkinter variable (like tk.StringVar) that stores the current selection.
values are the list of options that will appear in the menu.

Parameters:

activebackground
Syntax: option_menu_widget.config(activebackground="color")
Description: Background color of the selected item when active or hovered.
Default: SystemButtonFace
Example: option_menu_widget.config(activebackground="lightblue")
activeforeground
Syntax: option_menu_widget.config(activeforeground="color")
Description: Text color of the selected item when active or hovered.
Default: SystemButtonText
Example: option_menu_widget.config(activeforeground="white")
anchor
Syntax: option_menu_widget.config(anchor="position")
Description: Controls position of the text in the widget (e.g., "center", "w").
Default: center
Example: option_menu_widget.config(anchor="w")
background or bg
Syntax: option_menu_widget.config(bg="color")
Description: Background color of the menu.
Default: SystemButtonFace
Example: option_menu_widget.config(bg="white")
bd or borderwidth
Syntax: option_menu_widget.config(bd=value)
Description: Border width of the widget in pixels.
Default: 2
Example: option_menu_widget.config(bd=4)
bitmap
Syntax: option_menu_widget.config(bitmap="bitmap")
Description: Specifies a bitmap to display in place of text.
Default: None
Example: option_menu_widget.config(bitmap="warning")
cursor
Syntax: option_menu_widget.config(cursor="cursor_type")
Description: Changes the cursor when hovering over the menu.
Default: None
Example: option_menu_widget.config(cursor="hand2")
direction
Syntax: option_menu_widget.config(direction="direction")
Description: Specifies where the menu opens relative to the widget.
Default: below
Example: option_menu_widget.config(direction="above")
disabledforeground
Syntax: option_menu_widget.config(disabledforeground="color")
Description: Text color when the widget is disabled.
Default: SystemDisabledText
Example: option_menu_widget.config(disabledforeground="gray")
fg or foreground
Syntax: option_menu_widget.config(fg="color")
Description: Text color in the menu.
Default: SystemButtonText
Example: option_menu_widget.config(fg="blue")
font
Syntax: option_menu_widget.config(font=("FontName", size, style))
Description: Font of the text in the menu.
Default: TkDefaultFont
Example: option_menu_widget.config(font=("Arial", 12, "italic"))
height
Syntax: option_menu_widget.config(height=value)
Description: Height of the menu in number of lines.
Default: 0 (auto)
Example: option_menu_widget.config(height=2)
highlightbackground
Syntax: option_menu_widget.config(highlightbackground="color")
Description: Highlight color around the menu when it has focus.
Default: SystemButtonFace
Example: option_menu_widget.config(highlightbackground="orange")
highlightcolor
Syntax: option_menu_widget.config(highlightcolor="color")
Description: Color of the highlight border when focused.
Default: SystemWindowFrame
Example: option_menu_widget.config(highlightcolor="red")
highlightthickness
Syntax: option_menu_widget.config(highlightthickness=value)
Description: Thickness of the focus highlight border.
Default: 2
Example: option_menu_widget.config(highlightthickness=3)
image
Syntax: option_menu_widget.config(image=image_object)
Description: Specifies an image to display in place of text.
Default: None
Example: option_menu_widget.config(image=my_image)
indicatoron
Syntax: option_menu_widget.config(indicatoron=boolean)
Description: Displays or hides the indicator triangle.
Default: 1 (on)
Example: option_menu_widget.config(indicatoron=False)
justify
Syntax: option_menu_widget.config(justify="alignment")
Description: Specifies text alignment (left, center, or right).
Default: center
Example: option_menu_widget.config(justify="left")
Syntax: option_menu_widget["menu"]
Description: Accesses the menu widget for customization.
Default: .!optionmenu.menu
Example: option_menu_widget["menu"].config(bg="lightgray")
padx
Syntax: option_menu_widget.config(padx=value)
Description: Horizontal padding around the text.
Default: 5
Example: option_menu_widget.config(padx=10)
pady
Syntax: option_menu_widget.config(pady=value)
Description: Vertical padding around the text.
Default: 4
Example: option_menu_widget.config(pady=8)
relief
Syntax: option_menu_widget.config(relief="style")
Description: Specifies the border style (e.g., "raised", "sunken").
Default: raised
Example: option_menu_widget.config(relief="flat")
compound
Syntax: option_menu_widget.config(compound="position")
Description: Specifies the position of text relative to an image.
Default: none
Example: option_menu_widget.config(compound="left")
state
Syntax: option_menu_widget.config(state="state")
Description: Controls the widget’s state (e.g., "normal", "disabled").
Default: normal
Example: option_menu_widget.config(state="disabled")
takefocus
Syntax: option_menu_widget.config(takefocus=boolean)
Description: Specifies whether the widget can take focus.
Default: 0
Example: option_menu_widget.config(takefocus=1)
text
Syntax: option_menu_widget.config(text="text")
Description: Sets the default text for the menu.
Default: Option 1
Example: option_menu_widget.config(text="Select an option")
textvariable
Syntax: option_menu_widget.config(textvariable=tk.StringVar)
Description: Variable linked to the displayed text.
Default: PY_VAR0
Example: option_menu_widget.config(textvariable=my_var)
underline
Syntax: option_menu_widget.config(underline=index)
Description: Underlines the character at the specified index.
Default: -1 (no underline)
Example: option_menu_widget.config(underline=0)
width
Syntax: option_menu_widget.config(width=value)
Description: Width of the menu in number of characters.
Default: 0 (auto)
Example: option_menu_widget.config(width=10)
wraplength
Syntax: option_menu_widget.config(wraplength=value)
Description: Specifies the wrap width of the text in pixels.
Default: 0 (no wrap)
Example: option_menu_widget.config(wraplength=100)

12.6. Default options

Code to get the defaults for each optionmenu option is below.
import tkinter as tk

root = tk.Tk()

variable = tk.StringVar(value="One")
option_menu = tk.OptionMenu(root, variable, "One", "Two")

for option in option_menu.keys():
    print(f"{option}: {option_menu.cget(option)}")