Signup/Sign In

Python Tkinter Menu Widget

In this tutorial, we will cover the Tkinter Menu widget in Python which is used to create Menus with options for users to choose from.

The Tkinter Menu widget is used to create different types of menus in Python Application.

  • The following types of menus can be created using the Tkinter Menu widget: pop-up, pull-down, and top level.

  • Top-level menus are those menus that are displayed just under the title bar of the root or any other top-level windows. For example, all the websites have a top navigation menu just below the URL bar in the browser.

  • Menus are commonly used to provide convenient access to options like opening any file, quitting any task, and manipulating data in an application.

Tkinter Menu Widget

The syntax of the Menu widget is given below:

W = Menu(master, options)   

In the above syntax, the master parameter denotes the parent window. You can use many options to change the look of the menu and these options are written as comma-separated key-value pairs.

Tkinter Menu Widget Options:

Following are the various options used with Menu widgets:

Name of the Option Description
activebackground This option is used to indicate the background color of the widget when the widget is under the focus.
activeforeground This option indicates the font color of text of the widget when the widget has the focus.
activeborderwidth This option is used to indicate the width of the border of the widget when it is under the mouse(when it is active). The default value of this option is 1 pixel.
bd This option is used to indicate the border width of the widget
bg This option indicates the background color of the widget.
cursor This option indicates the cursor when the mouse hovers the menu.
disabledforeground This option indicates the text color of the widget when the widget is disabled
font This option is used to indicate the font type of the text of widget
fg This option specifies the foreground color of the widget.
relief This option is used to specify the border type. Its default value is RAISED.
image This option is used to display an image on the menu
postcommand This option can be set to any of the function which is called when the mouse hovers the menu.
tearoff The choices in the menu start from position 1 by default. But If we set the tearoff=1, then choices will start taking place from 0th position.
selectcolor This option indicates the color used to display the checkbutton or radiobutton when they are selected.
title This option is set to the title of the window if you want to change the title of the window.

Tkinter Menu Widget Methods:

Following are the various methods used with Tkinter Menu widget:

Name of method Description
add_command() This method is used to add menu items to the menu.
add_radiobutton() This method is used to add the radiobutton to the menu.
add_checkbutton() This method is mainly used to add checkbuttons to the menu.
add_cascade() This method is used to create a hierarchical menu to the parent menu by associating the given menu to the parent menu.
add_seperator() This method is used to add the separator line to the menu items.
add(type, options) This method is used to add the specific menu item to the menu.
delete(startindex, endindex) This method is used to delete the menu items that exist in the specified range.
entryconfig(index, options) This method is used to configure a menu item that is identified by the given index.
index(item) This method is used to get the index of the specified menu item.
insert_seperator(index) This method is used to insert a separator at the specified index.
invoke(index) This method is used to invoke the associated operation with the choice given at the specified index.
type(index) This method is used to get the type of choice specified by the index.

As you can see above we have so many methods associated to the Menu widget which can be used to configure the menu as per your requirement.

Tkinter Menu Widget Example

Below we have a basic example using this widget:

from tkinter import *

root = Tk()

def hello():
    print("hello!")

menubar = Menu(root)
menubar.add_command(label="Hello StudyTonight!", command=hello)
menubar.add_command(label="Quit!", command=root.quit)

root.config(menu=menubar)

root.mainloop()

After running the above code, you will see the above output. Whenever you will click on Hello StudyTonight! menu item then it will print a hello! on your console. While on clicking the Quit! menu item, the application will close.

Summary:

In this tutorial, we learned how to create a menu for our Tkinter application and how to add menu items to it and perform some operation when the user selects any menu items.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight