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 NameDescription
anchorControls 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 / bgSets the background color of the widget, Default Is platform specific
borderwidth / bdThe width of the widget’s border. The default is system specific but usually 1 to 2 pixels.
cursorThe cursor to display when the mouse moves over the widget. By default, the standard cursor is used.
disabledforegroundThe foreground color to use when the widget is disabled or invalid. The default is system specific.
fontFont to use for the widget. The default is system specific.
foreground / fgThe foreground color for this widget. The default is system specific.
activebackgroundSets the active color for menu button when it is active.
activeborderwidthSets the border width for the menu button when it is active. Default is 0
activeforegroundSets foreground color when the menu button is active.
imageImage to display on the widget. The value must be PhotoImage, BitmapImage, or compatible object. If specified, this overrides text and bitmap options.
padxHorizontal padding to add around the text. The default is 1 pixel.
padyVertical padding to add around the text. The default is 1 pixel.