7. tk Optionmenu
7.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 OptionMenu consists of a main button and a dropdown menu.
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.
7.2. 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 style
fontStyle1 = font.Font(family="Arial", size=16, weight="bold")
# Define the font style
fontStyle2 = font.Font(family="Arial", size=14, weight="normal")
# Create the OptionMenu widget
option_menu = tk.OptionMenu(root, fruit_var, *fruits)
option_menu.config(font=fontStyle1, bg="light green", fg="black", activebackground="dark green", activeforeground="white")
option_menu["menu"].config(font=fontStyle2, 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
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
fontStyle1 = font.Font(family="Arial", size=16, weight="bold")
# Define the font style
fontStyle2 = font.Font(family="Arial", size=14, weight="normal")
# Create the OptionMenu widget
option_menu = tk.OptionMenu(root, fruit_var, *fruits)
option_menu.config(font=fontStyle1, bg="light yellow", fg="blue", activebackground="orange", activeforeground="red")
option_menu["menu"].config(font=fontStyle2, bg="light pink", fg="purple", activebackground="yellow", activeforeground="green")
option_menu.pack(pady=10, padx=10)
# Run the main event loop
root.mainloop()
7.3. 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)
7.4. Default options
Code to get the defaults for each button option is below.
import tkinter as tk
root = tk.Tk()
button = tk.Button(root)
button_options = button.keys()
for option in button_options:
print(f"{option}: {button.cget(option)}") # cget retrieves the current value of the option