Python Tkinter Menu

By | April 10, 2020

In this tutorial, we will learn about Python Tkinter Menu. Tkinter Menu helps us to add a menu in the Python application. we can create three kinds of the menu using Menu widget which is: pop-up, top-level and pull-down.

Syntax to add Python Tkinter Menu:

w = Menu (master, options)  
  • master: This represents the parent window.
  • optionsHere is the list of most commonly used options for this widget.

Example Program of Python Tkinter Menu:

from tkinter import *  
 
mainWindow = Tk()  

mainWindow.geometry("300x150")

menubar = Menu(mainWindow)

filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New")
filemenu.add_command(label="Open")
filemenu.add_command(label="Save")
filemenu.add_command(label="Save as")

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

editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo")
editmenu.add_command(label="Cut")
editmenu.add_command(label="Copy")
editmenu.add_command(label="Paste")
editmenu.add_command(label="Delete")
editmenu.add_command(label="Select All")

menubar.add_cascade(label="Edit", menu=editmenu)

mainWindow.config(menu=menubar)

mainWindow.mainloop()

Output:

Python Tkinter menu

Various possible options in Python Tkinter Menu:

OptionDescription
activebackgroundThis option is used to set background color of widget under focus.
activeborderwidthThis option is used to set width of border when widget under mouse. By default it is 1px.
activeforegroundThis option is used to set foreground color of widget under focus.
bgThis option helps us to set normal background color of widget.
bdThis option helps us to set the border size around widget.
cursorThis option helps us to set the style of cursor like an arrow, dot etc.
disabledforegroundThis option is used to disable text color of widget.
fontThis option is used to set font type in widget.
fgThis option helps us to set normal foreground colour of widget.
reliefThis helps us to set the style of the border by which is default Flat.
selectcolorThis option is used to set color of selected RadioButton or CheckBox.
titleThis option is used to set the title of the window.

Python Tkinter Menu Functions:

There are various Python Tkinter Menu functions available. Which makes it so easy to use entry widget in our application these functions are as follow.

FunctionDescription
add_command(options)This function is used to add menu items in menu widget.
add_radiobutton(options)This function is used to add radiobuttons in menu widget.
add_checkbutton(options)This function is used to add checkbuttons in menu button.
add_cascade(options)This function is used to add one menu into another menu to create hierarchical menu.
add_seperator()This function is used to create separator line in menu.
add(type, options)This function is used to add a specific menu item in menu widget.
delete(startindex, endindex)This function is used to delete all the menu items from menu which falls between specified range,
entryconfig(index, options)This function is used to modify(changing options) menu item at given index.
index(item)This function is used to get the index number of specified menu item.
insert_seperator(index)This function is used to add separator at a specified index.
type(index)This function returns the type of specified index item such as “cascade”, “checkbutton”, “command”, “radiobutton”, “separator”, or “tearoff”.