12. tk Optionmenu
12.1. Usage
- 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 button that displays the current selection
the dropdown Menu containing the available options
- option_menu_widget.config(option=value)
- Allows you to set one or more option=value pairs to customize the widget.
- 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
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
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.Tasks
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".
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.valueshould 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: SystemButtonFaceExample: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: SystemButtonTextExample: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: centerExample:option_menu_widget.config(anchor="w")
- background or bg
- Syntax:
option_menu_widget.config(bg="color")Description: Background color of the menu.Default: SystemButtonFaceExample: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: 2Example: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: NoneExample: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: NoneExample: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: belowExample:option_menu_widget.config(direction="above")
- disabledforeground
- Syntax:
option_menu_widget.config(disabledforeground="color")Description: Text color when the widget is disabled.Default: SystemDisabledTextExample:option_menu_widget.config(disabledforeground="gray")
- fg or foreground
- Syntax:
option_menu_widget.config(fg="color")Description: Text color in the menu.Default: SystemButtonTextExample: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: TkDefaultFontExample: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: SystemButtonFaceExample:option_menu_widget.config(highlightbackground="orange")
- highlightcolor
- Syntax:
option_menu_widget.config(highlightcolor="color")Description: Color of the highlight border when focused.Default: SystemWindowFrameExample:option_menu_widget.config(highlightcolor="red")
- highlightthickness
- Syntax:
option_menu_widget.config(highlightthickness=value)Description: Thickness of the focus highlight border.Default: 2Example: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: NoneExample: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: centerExample:option_menu_widget.config(justify="left")
- menu
- Syntax:
option_menu_widget["menu"]Description: Accesses the menu widget for customization.Default: .!optionmenu.menuExample:option_menu_widget["menu"].config(bg="lightgray")
- padx
- Syntax:
option_menu_widget.config(padx=value)Description: Horizontal padding around the text.Default: 5Example:option_menu_widget.config(padx=10)
- pady
- Syntax:
option_menu_widget.config(pady=value)Description: Vertical padding around the text.Default: 4Example:option_menu_widget.config(pady=8)
- relief
- Syntax:
option_menu_widget.config(relief="style")Description: Specifies the border style (e.g., "raised", "sunken").Default: raisedExample: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: noneExample: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: normalExample:option_menu_widget.config(state="disabled")
- takefocus
- Syntax:
option_menu_widget.config(takefocus=boolean)Description: Specifies whether the widget can take focus.Default: 0Example:option_menu_widget.config(takefocus=1)
- text
- Syntax:
option_menu_widget.config(text="text")Description: Sets the default text for the menu.Default: Option 1Example: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_VAR0Example: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
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)}")