EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Tkinter Tutorial Tkinter Menu
Secondary Sidebar
Tkinter Tutorial
  • Tkinter
    • Python Tkinter
    • Tkinter Widgets
    • Tkinter background image
    • Tkinter button color
    • Tkinter place
    • Python Tkinter Button
    • Python Tkinter Canvas
    • Tkinter Frame
    • Tkinter LabelFrame
    • Python Tkinter Label
    • Tkinter Text
    • Tkinter Scrollbar
    • Tkinter Listbox
    • Tkinter Spinbox
    • Tkinter Checkbutton
    • Tkinter Menu
    • Tkinter Menubutton
    • Tkinter OptionMenu
    • Tkinter Messagebox
    • Tkinter Grid
    • Tkinter Pack
    • Tkinter Scale
    • Tkinter Table
    • Python Tkinter Entry
    • Tkinter after
    • Tkinter Colors
    • Tkinter Font
    • Tkinter PhotoImage
    • Tkinter TreeView
    • Tkinter Notebook
    • Tkinter Combobox
    • Tkinter Bind
    • Tkinter Icon
    • Tkinter Window Size
    • Tkinter Color Chart
    • Tkinter Slider
    • Tkinter Calculator
    • Tkinter geometry
    • Tkinter image
    • Tkinter input box
    • Tkinter mainloop

Tkinter Menu

By Priya PedamkarPriya Pedamkar

Tkinter Menu

Overview of Tkinter Menu

Tkinter Menu is a widget that helps in creating a set of action items that are going to be used in the application. Tkinter is a standard package in python. To create multiple choices in terms of symbols and text, the menu helps with that. The moment any of the choices is clicked or touched, an action is activated, which could be in terms of any operation like opening a file, saving a file, etc.

There is two popular variety of Menu, we will discuss here:

  1. Pull-down
  2. Pop-up

Syntax:

Below shown is the syntax to create any Menu widget:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,697 ratings)
w = Menu ( master, option, ... )

Master: The parent window, where the widget needs to be imbibed.

Option: The various parameter related to the widget used.

Menu Methods with Examples

The different methods are given below:

1. Pull-down Menu

Let’s discuss the first type of menu, i.e. Pulldown Menu.

Code:

from tkinter import *
Master = Tk()
variable = StringVar(Master)
variable.set("Open") # default value
wi = OptionMenu(Master, variable, "Open", "save", "save as")
wi.pack()
mainloop()

Output:

tkinter menu - pull down

As one can see, “OptionMenu” is used from the Tkinter package to implement a pull-down menu. Tk helps in the creation of the top-level window. Here “OptionMenu” will pop up with a button. To which, we have set a default value of “Open”. Rest other values “save” & “save as” can be selected from the dropdown. If further someone wants action to be associated with it, one can use the .get() function to accomplish it.

As one can notice, as we had only three options. However, we easily put it in “OptionMenu”; however, if someone has a great list of things to be showcased under “OptionMenu”,
Then here is the approach:

Code:

from tkinter import *
Items = [
    "egg",
    "toffee",
    "chicken",
    "beef",
    "bread",
    "Choclate",
    "Milk"
]
Master = Tk()
click = StringVar(Master)
click.set(OPTIONS[0]) # default value
wi = OptionMenu(Master,click,*Items)
wi.pack()
mainloop()

Output:

tkinter menu - pull down 1.1

In order to pass the list in “OptionMenu”, the list should be prefixed with an asterisk symbol. This code has easy maintenance, as one can simply add, remove or edit any item in the list quickly. Plus, the code looks less clumsy and more readable.
One can even add a submenu to the menu.

Code:

from tkinter import *
from tkinter import filedialog
def Donothing():
    x = 0
root = Tk()
menu = Menu(root)
root.config(menu=menu)
filemainmenu = Menu(menu, tearoff=0)
filesubmenu = Menu(filemainmenu, tearoff=0)
menu.add_cascade(label="File", menu=filemainmenu)
filemainmenu.add_cascade(label="Save", menu=filesubmenu)
filesubmenu.add_command(label="Save Nothing", command = Donothing)
root.mainloop()

Output:

 tkinter menu- example3

tkinter menu- example3.1

As one can see, “Save Nothing” is a submenu of the “Save” Label.

With the help of “add_cascade”, one can create a sub-menu under the main menu. If you notice well, the “filesubmenu = Menu(filemainmenu, tearoff=0)” command is used to associate filesubmenu with filemainmenu. One can add a seperator as well between the options. Like:

Code:

from tkinter import *
from tkinter import filedialog
def Donothing():
    x = 0
root = Tk()
menu = Menu(root)
root.config(menu=menu)
filemainmenu = Menu(menu, tearoff=0)
filesubmenu = Menu(filemainmenu, tearoff=0)
menu.add_cascade(label="File", menu=filemainmenu)
filemainmenu.add_cascade(label="Save", menu=filesubmenu)
filesubmenu.add_command(label="Save Nothing", command = Donothing)
filemainmenu.add_separator()
filemainmenu.add_command(label="Exit", command=root.quit)
root.mainloop()

Output:

Save nothing

If someone notices well, we used the command: “filemainmenu.add_seperator()”. Its sole purpose is to add a line of separation to the menu.

There are many methods related to the drop-down menu. One needs to experiment with all in order to get a good grip over Menu designing with Tkinter.

2. Pop-up Menu

The pop-up menu is the menu shown anywhere in the window. It is also known as the context menu. Let’s understand it through the code below.

Code:

from tkinter import *
def Hi():
    message.config(text = "Hi")
def Hey():
    message.config(text = "Hey")
root = Tk()
root.title("This is an Example: PopUp Menu")
#w = Label(root, text="Right-click to display menu", width=40, height=20)
#w.pack()
message = Label(root,bg = "grey")
message.pack(fill = BOTH,expand = YES)
# create a menu
popup = Menu(root, tearoff=0)
popup.add_command(label="Print Hi",command = Hi) 
popup.add_command(label="Print Hey",command = Hey)
def perform_popup(event):    # To display the popup menu
    try:
        popup.tk_popup(event.x_root, event.y_root, 0)
    finally:
        popup.grab_release()
message.bind("", perform_popup)
mainloop()

Output #1

Pop-up menu 1

Output #2

Pop-up menu 2

As one can see, how to pop up window is used to print “Hi” or “Hey” accordingly, with the help of functions defined. First, the window is created with the help of the Tkinter root(which is the top-level window), and then a pop-menu is added to it with the help of “Menu(root)”

When tearoff = 0, it means menu options will be added at the starting position. When one right clicks, it opens the 2-D popup menu with options listed over the window. The label one clicks, action triggers as per that which we call event.

Command:

"popup.tk_popup(event.x_root, event.y_root, 0)" will post the pop up at location "x_root" and "y_root"

Suppose someone doesn’t want any action to be associated with the label. One can opt for it by simply doing nothing.

Code:

from tkinter import *
def Donothing():
    x = 0    
def Hey():
    message.config(text = "Hey")   
root = Tk()
root.title("This is an Example: PopUp Menu")
#w = Label(root, text="Right-click to display menu", width=40, height=20)
#w.pack()
message = Label(root,bg = "grey")
message.pack(fill = BOTH,expand = YES)
# create a menu
popup = Menu(root, tearoff=0)
popup.add_command(label="Print Hi",command = Donothing) 
popup.add_command(label="Print Hey",command = Hey)
def perform_popup(event):    # To display the popup menu
    try:
        popup.tk_popup(event.x_root, event.y_root, 0)
    finally:
        popup.grab_release()
message.bind("", perform_popup)	
mainloop()

Output:

Example 4

Conclusion

As we saw above, the Tkinter menu and its various usage-based on certain examples. The examples shown above are straightforward and easy to grab. Tkinter menu is of great help in building applications with various options as part of it. One can embed options in a drop down or pop up menu as per requirement and hence associate it with action items. There are many methods available with the menu; one must get their hands dirty to understand it fully.

Recommended Articles

This is a guide to Tkinter Menu. Here we discuss the overview, Syntax, Menu Methods and Examples along with the codes & outputs. You can also go through our other suggested articles to learn more –

  1. Tkinter LabelFrame
  2. Python Statistics Module
  3. Tkinter Listbox
  4. Python Find String
  5. Tkinter Grid | Examples
  6. Guide to Tkinter Widgets
  7. Examples of Tkinter Messagebox
Popular Course in this category
Python Certifications Training Program (40 Courses, 13+ Projects)
  40 Online Courses |  13 Hands-on Projects |  215+ Hours |  Verifiable Certificate of Completion
4.8
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more