MENU IN TKINTER


In this tutorial, we are going to learn about the menu widget in Tkinter, we will learn about the essential menu that is used for application’s main function. Further, we will learn about the features and methods followed by a handy example.





What is Menu in Tkinter?

A normal menu in Tkinter is used to implement options for performing various functions related to the Tkinter (GUI) application that you want to create. A ‘menu’ is also known as a top level menu which provides options to open, save, edit or quit the application. But it’s not limited to just a top level menu, you can create a pop-up and pull-down menu too.

menu_tk = Menu( window, features )

Menu features and properties are:

1 activebackground Shows the menu background color upon the mouse focus
2 activeborderwidth Shows the menu border color upon the mouse hover
3 activeforeground The font color of the widget when the widget has the focus.
4 bg It shows the background color of the menu. 
5 bd It shows the width of the border. Same as borderwidth. Default value is 2px.
6 cursor Shows the type of cursor when the mouse is hovered over the label. The default is a pointed arrow cursor.
7 disabledforeground This will specify the foreground color when the widget is disabled.
8 font It decides the font of the button text.
9 fg Shows the foreground color of the menu 
10 postcommand You can set any function to the postcommand method and later can call it when the mouse hovers over the menu.
11 relief This displays different types of borders like SUNKEN, RAISED, GROOVE, and RIDGE.
12 image It is used to set image on the menu 
13 selectcolor Shows the color of the checkbutton or radiobutton when selected.
14 tearoff The point or position from where the menu starts.
15 title Used to set title for the window of your GUI application.

Methods used for menu are:

1 add_command(options) Used to add menu items to the main menu.
2 add_radiobutton(options) Used for radiobutton addition.
3 add_checkbutton(options) Used for checkbuttons addition.
4 add_cascade(options) It is used to create a sub-level menu corresponding to its parent menu.
5 add_seperator() Adds a separate line between the menu items.
6 add(type, options) Adds a specified menu item to the main menu.
7 delete(startindex, endindex) Deletes menu items within the specified range.
8 entryconfig(index, options) Configures menu items through the index provided.
9 index(item) Fetches the index number of the menu items.
10 insert_seperator(index) Adds a separate line between the menu item index wise.
11 invoke(index) It is used to request the menu item that we want we at a specified index.
12 type(index) It is used to fetch the menu type that we want we at a specified index.




Creating Menu Widget

You can create a simple menu as follows:

from tkinter import * 
from tkinter.ttk import * 
from time import strftime    
screen = Tk()

screen.geometry('400x400')

Menubar 
menubar = Menu(screen) 

#Filemenu
file = Menu(menubar, tearoff = 1) 
menubar.add_cascade(label ='File', menu = file) 
file.add_command(label ='New File', command = None) 
file.add_command(label ='Open...', command = None) 
file.add_command(label ='Save', command = None) 
file.add_separator() 
file.add_command(label ='Exit', command = screen.destroy) 
  
#Editmenu 
edit = Menu(menubar, tearoff = 1) 
menubar.add_cascade(label ='Edit', menu = edit) 
edit.add_command(label ='Cut', command = None) 
edit.add_command(label ='Copy', command = None) 
edit.add_command(label ='Paste', command = None) 
edit.add_command(label ='Select All', command = None) 
edit.add_separator() 
edit.add_command(label ='Find...', command = None) 
edit.add_command(label ='Find again', command = None) 
  
#Helpmenu
help_ = Menu(menubar, tearoff = 1) 
menubar.add_cascade(label ='Help', menu = help_) 
help_.add_command(label ='Tk Help', command = None) 
help_.add_command(label ='Demo', command = None) 
help_.add_separator() 
help_.add_command(label ='About Tk', command = None)
screen.config(menu = menubar) 

mainloop()

Output will be:

menu-tkinter

References have been used from here