10. tk Menubutton
10.1. Overview
The
tk.Menubutton widget creates a button that displays a menu when clicked.It is useful for creating drop-down buttons, toolbar menus, and option selectors.
Unlike
tk.Menu, a Menubutton is a visible widget placed inside a window.To create a Menubutton widget, the general syntax is
(assuming import via "import tkinter as tk"):
- menubutton_widget = tk.Menubutton(parent, option=value)
- *
parentis the window or frame object.*option=valuespecifies one or more configuration options.
A menu is attached to a Menubutton using
menu=menu_widget.- menu_widget = tk.Menu(parent, option=value)
- *
parentis the menubutton widget.*option=valuespecifies one or more configuration options.
10.2. Sample Menubutton
The code below creates a Menubutton with a drop-down menu.
import tkinter as tk
root = tk.Tk()
root.title("Menubutton Example")
root.geometry("300x200")
menubutton = tk.Menubutton(
root,
text="Options",
relief="raised"
)
# Create the menu owned by the Menubutton
menu = tk.Menu(menubutton, tearoff=0)
menu.add_command(label="Option 1")
menu.add_command(label="Option 2")
menu.add_separator()
menu.add_command(label="Exit", command=root.destroy)
# Attach the menu
menubutton.config(menu=menu)
menubutton.pack(padx=20, pady=20)
root.mainloop()
Tasks
Modify the code to create the Menubutton shown below.
Modify the code so it creates the Menubutton shown above.
import tkinter as tk
root = tk.Tk()
root.title("Coloured Menubutton Example")
root.geometry("300x200")
# Create the Menubutton
menubutton = tk.Menubutton(
root,
text="Colours",
relief="raised"
)
# Create the menu
colour_menu = tk.Menu(menubutton, tearoff=0)
colour_menu.add_command(label="Red")
colour_menu.add_command(label="Green")
colour_menu.add_command(label="Blue")
colour_menu.add_separator()
colour_menu.add_command(label="Exit", command=root.destroy)
# Colour individual menu entries
colour_menu.entryconfig("Red", foreground="red")
colour_menu.entryconfig("Green", foreground="green")
colour_menu.entryconfig("Blue", foreground="blue")
menubutton.config(menu=colour_menu)
menubutton.pack(padx=20, pady=20)
root.mainloop()
10.3. Common Menubutton Methods
10.4. Menubutton methods
Menubutton inherits many methods from the standard Tk widget class.The drop-down menu behaviour is controlled by the attached
Menu widget.Menu entry methods are called on the associated
Menu object, not the Menubutton itself.- menubutton_widget.config(option=value)
- Changes one or more
Menubuttonconfiguration options.Common options includetext,font,bg,fg,relief,directionandmenu.Example:menubutton_widget.config(text="Options")
- menubutton_widget.configure(option=value)
- Updates one or more
Menubuttonconfiguration options.This is an alternative name forconfig().Example:menubutton_widget.configure(bg="lightblue")
- value = menubutton_widget.cget(option)
- Returns the current value of a
Menubuttonconfiguration option.Example:colour = menubutton_widget.cget("text")
- menubutton_widget.flash()
- Flashes the
Menubuttonseveral times.Useful for drawing attention to the widget.The visual effect depends on the current widget style and theme.
- result = menubutton_widget.invoke()
- Activates the
Menubuttonas if it had been clicked.Opens the associated menu.Returns the result of the associated command, if one exists.
- menu_widget.entryconfig(index, option=value)
- Changes the configuration of an existing menu entry.This method is called on the attached
Menuobject.indexcan be a menu position number or the entry label.Example:colour_menu.entryconfig("Red", foreground="red")Common options include:-foregroundorfg: changes the text colour.-backgroundorbg: changes the menu item background colour.-font: changes the menu item font.-state: enables or disables the menu item.-label: changes the displayed text.
- menu_widget.entryconfigure(index, option=value)
- Alternative name for
entryconfig().Updates the configuration of an existing menu entry.Example:colour_menu.entryconfigure("Blue", state="disabled")
- menu_widget.add_command(label=text, command=function)
- Adds a normal command item to the menu.Example:
colour_menu.add_command(label="Red")
- menu_widget.add_separator()
- Adds a horizontal separator line between menu entries.Example:
colour_menu.add_separator()
- menu_widget.add_checkbutton(label=text, variable=tk_var)
- Adds a checkbutton item to the menu.The item stores an on/off state using a Tkinter control variable.Example:
colour_menu.add_checkbutton(label="Show Grid", variable=show_grid)
- menu_widget.add_radiobutton(label=text, variable=tk_var, value=value)
- Adds a radio button item to the menu.Radio menu items normally share the same control variable.Example:
colour_menu.add_radiobutton(label="Red", variable=colour, value="red")
- menu_widget.add_cascade(label=text, menu=submenu)
- Adds a submenu to the menu.Used to create nested menus.Example:
main_menu.add_cascade(label="File", menu=file_menu)
- menu_widget.delete(index1, index2=None)
- Removes one or more menu entries.Example:
colour_menu.delete(0)
- menu_widget.entrycget(index, option)
- Returns the current value of an option for a specific menu entry.Example:
colour = colour_menu.entrycget("Red", "foreground")
- menu_widget.index(index)
- Returns the numerical position of a menu entry.Example:
position = colour_menu.index("Blue")
- menu_widget.post(x, y)
- Displays the menu at a specific screen position.Commonly used to create custom popup menus.Example:
colour_menu.post(100, 100)
- menu_widget.unpost()
- Removes a posted menu from the screen.Used after menus displayed manually with
post().
10.5. Parameter syntax
- menubutton_widget = tk.Menubutton(parent, option=value)
Parameters:
- activebackground
- Syntax:
menubutton_widget.config(activebackground="color")Description: Background colour when the mouse is over the button.Default: SystemButtonFace
- activeforeground
- Syntax:
menubutton_widget.config(activeforeground="color")Description: Text colour when the mouse is over the button.Default: SystemButtonText
- anchor
- Syntax:
menubutton_widget.config(anchor="position")Description: Controls where the text is positioned inside the widget.Default: center
- background
- bg
- Syntax:
menubutton_widget.config(bg="color")Description: Sets the background colour.Default: SystemButtonFace
- bd
- borderwidth
- Syntax:
menubutton_widget.config(borderwidth=value)Description: Sets the border width.Default: 2
- compound
- Syntax:
menubutton_widget.config(compound="position")Description: Controls the position of text relative to an image.Default: none
- cursor
- Syntax:
menubutton_widget.config(cursor="cursor_type")Description: Sets the mouse cursor.Example:cursor="hand2"
- direction
- Syntax:
menubutton_widget.config(direction="direction")Description: Controls where the menu appears relative to the button.Possible values:above,below,left,right
- font
- Syntax:
menubutton_widget.config(font=("Font", size))Description: Sets the font used for the button text.
- foreground
- fg
- Syntax:
menubutton_widget.config(fg="color")Description: Sets the text colour.
- height
- Syntax:
menubutton_widget.config(height=value)Description: Sets the height of the widget.
- image
- Syntax:
menubutton_widget.config(image=image_object)Description: Displays an image on the button.
- indicatoron
- Syntax:
menubutton_widget.config(indicatoron=boolean)Description: Displays a small indicator arrow when enabled.Default: 1
- justify
- Syntax:
menubutton_widget.config(justify="alignment")Description: Controls multi-line text alignment.
- menu
- Syntax:
menubutton_widget.config(menu=menu_widget)Description: Attaches a Menu widget to the Menubutton.
- padx
- Syntax:
menubutton_widget.config(padx=value)Description: Sets horizontal internal padding.
- pady
- Syntax:
menubutton_widget.config(pady=value)Description: Sets vertical internal padding.
- relief
- Syntax:
menubutton_widget.config(relief="style")Description: Sets the border style.Default: raised
- state
- Syntax:
menubutton_widget.config(state="state")Description: Sets the widget state.Values:normalordisabled
- text
- Syntax:
menubutton_widget.config(text="label")Description: Sets the displayed text.
- textvariable
- Syntax:
menubutton_widget.config(textvariable=variable)Description: Associates a Tkinter variable with the button text.
- underline
- Syntax:
menubutton_widget.config(underline=index)Description: Underlines the specified character in the text.
- width
- Syntax:
menubutton_widget.config(width=value)Description: Sets the width of the widget.
10.6. Default options
Code to display the default values for each
Menubutton option is shown below.import tkinter as tk
root = tk.Tk()
widget = tk.Menubutton(root)
for option in widget.keys():
print(f"{option}: {widget.cget(option)}")