Python Tkinter Menu

Created with Sketch.

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.


  1. w = Menu(top, options)

A list of possible options is given below.

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.


The Menu widget contains the following methods.

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

  1. # !/usr/bin/python3
  2. from tkinter import *
  3. top = Tk()
  4. def hello():
  5.     print(“hello!”)
  6. # create a toplevel menu
  7. menubar = Menu(root)
  8. menubar.add_command(label=“Hello!”, command=hello)
  9. menubar.add_command(label=“Quit!”, command=top.quit)
  10. # display the menu
  11. top.config(menu=menubar)
  12. top.mainloop()


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

  1. from tkinter import Toplevel, Button, Tk, Menu
  2. top = Tk()
  3. menubar = Menu(top)
  4. file = Menu(menubar, tearoff=0)
  5. file.add_command(label=“New”)
  6. file.add_command(label=“Open”)
  7. file.add_command(label=“Save”)
  8. file.add_command(label=“Save as…”)
  9. file.add_command(label=“Close”)
  10. file.add_separator()
  11. file.add_command(label=“Exit”, command=top.quit)
  12. menubar.add_cascade(label=“File”, menu=file)
  13. edit = Menu(menubar, tearoff=0)
  14. edit.add_command(label=“Undo”)
  15. edit.add_separator()
  16. edit.add_command(label=“Cut”)
  17. edit.add_command(label=“Copy”)
  18. edit.add_command(label=“Paste”)
  19. edit.add_command(label=“Delete”)
  20. edit.add_command(label=“Select All”)
  21. menubar.add_cascade(label=“Edit”, menu=edit)
  22. help = Menu(menubar, tearoff=0)
  23. help.add_command(label=“About”)
  24. menubar.add_cascade(label=“Help”, menu=help)
  25. top.config(menu=menubar)
  26. top.mainloop()


Python Tkinter Menu


Leave a Reply

Your email address will not be published. Required fields are marked *