What is Menubutton widget?
MenuButton widget is used to create and display all kind of menus used by an application. MenuButton is previously used to create top-level menus, but in the newer version, only with Menu widget, we can create top-level menus.
Example
The following Python script creates a window containing one MenuButton widget with some drop down options.
You need to write following code in any text editor and then save it with .py extension. To execute the application, Type py MenubuttonExample.py command in terminal.
from tkinter import * # Top level window window = Tk() window.title("Studyfied.com") window.geometry('350x200') # Create a menu button menubutton = Menubutton(window, text="File") menubutton.grid() # Create pull down menu menubutton.menu = Menu(menubutton, tearoff = 0) menubutton["menu"] = menubutton.menu # Add some commands menubutton.menu.add_command(label="Create new") menubutton.menu.add_command(label="Open") menubutton.menu.add_separator() menubutton.menu.add_command(label="Exit") menubutton.pack() window.mainloop()
Output
The above code produces the following output in windows operating system.
Explanation
Following line creates a Tkinter window
from tkinter import * window = Tk() window.title("Studyfied.com") window.geometry('350x200')
See explanation of Tkinter window – Tkinter top level window
This snippet creates a menubutton, First parameter passed in MenuButton class is the parent window. Here it is “window”, You can specify more attributes to customize it. The next parameter text is used to display some text on the menu button.
# Create a menu button menubutton = Menubutton(window, text="File") menubutton.grid()
This snippet creates drop-down option menu, drop down menu becomes visible whenever the user clicks on the menu button.
# Create pull down menu menubutton.menu = Menu(menubutton, tearoff = 0) menubutton["menu"] = menubutton.menu
This snippet adds some commands to the drop-down menu, Similar to the menu widget.
# Add some commands menubutton.menu.add_command(label="Create new") menubutton.menu.add_command(label="Open") menubutton.menu.add_separator() menubutton.menu.add_command(label="Exit")
See how to use the menu widget – Tkinter menu widget
Some menubutton properties
Property Name | Description |
---|---|
anchor | Controls the position of the text (or image) within the widget. Valid values: N, NE, E, SE, S, SW, W, NW, or CENTER. The default is CENTER. |
background / bg | Sets the background color of the widget, Default Is platform specific |
borderwidth / bd | The width of the widget’s border. The default is system specific but usually 1 to 2 pixels. |
cursor | The cursor to display when the mouse moves over the widget. By default, the standard cursor is used. |
disabledforeground | The foreground color to use when the widget is disabled or invalid. The default is system specific. |
font | Font to use for the widget. The default is system specific. |
foreground / fg | The foreground color for this widget. The default is system specific. |
activebackground | Sets the active color for menu button when it is active. |
activeborderwidth | Sets the border width for the menu button when it is active. Default is 0 |
activeforeground | Sets foreground color when the menu button is active. |
image | Image to display on the widget. The value must be PhotoImage, BitmapImage, or compatible object. If specified, this overrides text and bitmap options. |
padx | Horizontal padding to add around the text. The default is 1 pixel. |
pady | Vertical padding to add around the text. The default is 1 pixel. |