Python Tkinter Menu

The Menu widget is used to create various types of menus (top level, pull down, and pop up) in the python application.

The top-level menus are the one which is displayed just under the title bar of the parent window. We need to create a new instance of the Menu widget and add various commands to it by using the add() method.

The syntax to use the Menu widget is given below.

Syntax

A list of possible options is given below.

SNOptionDescription
1activebackgroundThe background color of the widget when the widget is under the focus.
2activeborderwidthThe width of the border of the widget when it is under the mouse. The default is 1 pixel.
3activeforegroundThe font color of the widget when the widget has the focus.
4bgThe background color of the widget.
5bdThe border width of the widget.
6cursorThe mouse pointer is changed to the cursor type when it hovers the widget. The cursor type can be set to arrow or dot.
7disabledforegroundThe font color of the widget when it is disabled.
8fontThe font type of the text of the widget.
9fgThe foreground color of the widget.
10postcommandThe postcommand can be set to any of the function which is called when the mourse hovers the menu.
11reliefThe type of the border of the widget. The default type is RAISED.
12imageIt is used to display an image on the menu.
13selectcolorThe color used to display the checkbutton or radiobutton when they are selected.
14tearoffBy default, the choices in the menu start taking place from position 1. If we set the tearoff = 1, then it will start taking place from 0th position.
15titleSet this option to the title of the window if you want to change the title of the window.

Methods

The Menu widget contains the following methods.

SNOptionDescription
1add_command(options)It is used to add the Menu items to the menu.
2add_radiobutton(options)This method adds the radiobutton to the menu.
3add_checkbutton(options)This method is used to add the checkbuttons to the menu.
4add_cascade(options)It is used to create a hierarchical menu to the parent menu by associating the given menu to the parent menu.
5add_seperator()It is used to add the seperator line to the menu.
6add(type, options)It is used to add the specific menu item to the menu.
7delete(startindex, endindex)It is used to delete the menu items exist in the specified range.
8entryconfig(index, options)It is used to configure a menu item identified by the given index.
9index(item)It is used to get the index of the specified menu item.
10insert_seperator(index)It is used to insert a seperator at the specified index.
11invoke(index)It is used to invoke the associated with the choice given at the specified index.
12type(index)It is used to get the type of the choice specified by the index.

Creating a top level menu

A top-level menu can be created by instantiating the Menu widget and adding the menu items to the menu.

Example 1

Output:

Python Tkinter Menu

Clicking the hello Menubutton will print the hello on the console while clicking the Quit Menubutton will make an exit from the python application.

Example 2

Output:

Python Tkinter Menu