Tkinter

Menu Tkinter | Python 3

In this tutorial, we are going to see how to use Menu widget Tkinter. The Menu widget allows us to create all kinds of menus that can be used by our applications. The basic functionality allows creating three types of menus: pop-up, top-level, and pull-down.

It is also possible to use other widgets to implement new types of menus, such as the OptionMenu widget, which implements a special type that generates a contextual list of items in a selection.
 


 

Syntax:

Here is the syntax to create this widget:

menu = Menu ( master, option = value, ... )

 

Parameters:
  • master : This represents the parent window.
  • options : Options can be used as comma separated key-value pairs. See the following example:

 

Example:
from tkinter import *
  
gui = Tk()

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

# create a menu
menubar = Menu(gui)
# create a sub-menu
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=hello)
filemenu.add_command(label="Open", command=hello)
filemenu.add_command(label="Save", command=hello)

menubar.add_cascade(label="File", menu=filemenu)
menubar.add_command(label="Quit!", command=gui.quit)

# display the menu
gui.config(menu=menubar)
gui.mainloop()

Output:


 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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