The Menubutton widget in Tkinter creates a drop-down menu that allows users to select from a list of options. It's a useful component for applications that require menu-based navigation or options.
w = Menubutton(parent, options)
Here, parent is the parent widget (such as a window or frame), and options are used to configure the appearance and behavior of the Menubutton.
| SN | Option | Description |
|---|---|---|
| 1 | activebackground | Background color when the widget is under focus. |
| 2 | activeforeground | Font color when the widget is under focus. |
| 3 | anchor | Specifies the position of the content when the widget has more space than needed. |
| 4 | bg | Background color of the widget. |
| 5 | bitmap | Graphical content (bitmap) to be displayed on the widget. |
| 6 | bd | Size of the border in pixels. Default is 2 pixels. |
| 7 | cursor | Mouse pointer type when hovering over the widget (e.g., arrow, dot). |
| 8 | direction | Direction to display the menu. Options: LEFT, RIGHT, or ABOVE. |
| 9 | disabledforeground | Text color when the widget is disabled. |
| 10 | fg | Normal foreground color of the widget. |
| 11 | height | Vertical dimension of the Menubutton in lines. |
| 12 | highlightcolor | Highlight color when the widget is in focus. |
| 13 | image | Image displayed on the widget. |
| 14 | justify | Position of the text within the widget when it doesn't fill the width. Values: LEFT, RIGHT, CENTER. |
| 15 | menu | Menu associated with the Menubutton. |
| 16 | padx | Horizontal padding of the widget. |
| 17 | pady | Vertical padding of the widget. |
| 18 | relief | Type of border around the widget. Values: FLAT, RAISED, SUNKEN, GROOVE, RIDGE. Default is RAISED. |
| 19 | state | State of the Menubutton. Values: NORMAL, DISABLED. |
| 20 | text | Text displayed on the widget. |
| 21 | textvariable | Control variable for the text of the widget. |
| 22 | underline | Underline a specific character in the text. Set to the index of the character to underline. |
| 23 | width | Width of the widget in characters. Default is 20 characters. |
| 24 | wraplength | Breaks the text into multiple lines if it exceeds the specified width. |
Here is a simple example demonstrating how to use the Menubutton widget with a drop-down menu:
from tkinter import *
def show_selection():
selected = menu_var.get()
selection_label.config(text=f"Selected option: {selected}")
# Create the main window
root = Tk()
root.title("Tkinter Menubutton Example")
# Create a Menubutton
menu_var = StringVar()
menu_var.set("Select an option") # Default text
menubutton = Menubutton(root, textvariable=menu_var, relief=RAISED, width=20)
menubutton.pack(pady=10)
# Create a Menu associated with the Menubutton
menu = Menu(menubutton, tearoff=0)
menu.add_command(label="Option 1", command=lambda: menu_var.set("Option 1"))
menu.add_command(label="Option 2", command=lambda: menu_var.set("Option 2"))
menu.add_command(label="Option 3", command=lambda: menu_var.set("Option 3"))
menubutton.config(menu=menu)
# Create a Label to display the selected option
selection_label = Label(root, text="Selected option: None", font=("Arial", 12))
selection_label.pack(pady=10)
# Create a Button to show the selected option
show_button = Button(root, text="Show Selected", command=show_selection)
show_button.pack(pady=10)
# Run the application
root.mainloop()
• Menubutton: Displays a drop-down menu with options.
• Menu: Populates the Menubutton with a list of options. Each option updates the text of the Menubutton when selected.
• Label: Shows the selected option after clicking the "Show Selected" button.
• Button: When clicked, it updates the label to reflect the selected option.
The Menubutton widget is a versatile component for creating dropdown menus in your Tkinter applications, allowing users to select from a list of options conveniently.