What is Menu widget?

Menu widget is used to create and display all kind of menus used by an application. The menubar is something that stays always at top of the application window or just below the title bar of the window. In most of the applications, you see there is one menu bar with some options, we can implement the same with Menu widget.

Example 1

The following Python script creates a window containing one Menu widget with two commands “File” and “Edit”.

You need to write following code in any text editor and then save it with .py extension. To execute the application, Type py MenuWidgetExample.py command in terminal. 

from tkinter import *

# Top level window
window = Tk()

window.title("Studyfied.com")
window.geometry('350x200')

# Create a toplevel menu bar
menubar = Menu(window)
menubar.add_command(label="File")
menubar.add_command(label="Edit")

# Display the menu
window.config(menu=menubar)

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 menu bar, First parameter passed in Menu class is the parent window. Here it is “window”, You can specify more attributes to customize it. The next two lines are used to create/add some command for the menu. You can also create a pull-down menu by adding another menu that is pointing another parent menu as root.

# Create a toplevel menu bar
menubar = Menu(window)
menubar.add_command(label="File")
menubar.add_command(label="Edit")

Example 2

The following Python script creates a window containing one menu that has pull-down command menus, It is similar to the previous example but here top-level command “File” has drop-down options or commands.

from tkinter import *

# Top level window
window = Tk()

window.title("Studyfied.com")
window.geometry('350x200')

# Create a toplevel menu bar
menubar = Menu(window)

# Create file pulldown menus
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open")
filemenu.add_command(label="Save")
filemenu.add_separator()
filemenu.add_command(label="Exit", command=window.quit)
menubar.add_cascade(label="File", menu=filemenu)

# Create edit pulldown menus
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Cut")
editmenu.add_command(label="Copy")
editmenu.add_command(label="Paste")
menubar.add_cascade(label="Edit", menu=editmenu)

# Create help pulldown menu
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About")
menubar.add_cascade(label="Help", menu=helpmenu)

# display the menu
window.config(menu=menubar)

window.mainloop()

Output

The above code produces the following output in windows operating system.

Explanation

This line adds a separator between two commands.

filemenu.add_separator()

This line appends or adds menu to the parent or top-level menu, Here “filemenu” is the current menu that holds all the pull-down commands and “menubar” is the top level menu.

menubar.add_cascade(label="File", menu=filemenu)

Some menu properties

Property NameDescription
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.
buttoncursorCursor to display when the mouse moves over the commands Default is an arrow.
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 commands when it is active. The default is SystemHighlight.
activeborderwidthSets the border width for command when it is active. Default is 0
activeforegroundSets foreground color when the command is active. Default value is SystemHighlightText