C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python Tkinter MenuThe 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. Syntaxw = Menu(top, options) A list of possible options is given below.
Methods
The Menu widget contains the following methods.
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
# !/usr/bin/python3
from tkinter import *
top = Tk()
def hello():
print("hello!")
# create a toplevel menu
menubar = Menu(root)
menubar.add_command(label="Hello!", command=hello)
menubar.add_command(label="Quit!", command=top.quit)
# display the menu
top.config(menu=menubar)
top.mainloop()
Output:
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 2from tkinter import Toplevel, Button, Tk, Menu top = Tk() menubar = Menu(top) file = Menu(menubar, tearoff=0) file.add_command(label="New") file.add_command(label="Open") file.add_command(label="Save") file.add_command(label="Save as...") file.add_command(label="Close") file.add_separator() file.add_command(label="Exit", command=top.quit) menubar.add_cascade(label="File", menu=file) edit = Menu(menubar, tearoff=0) edit.add_command(label="Undo") edit.add_separator() edit.add_command(label="Cut") edit.add_command(label="Copy") edit.add_command(label="Paste") edit.add_command(label="Delete") edit.add_command(label="Select All") menubar.add_cascade(label="Edit", menu=edit) help = Menu(menubar, tearoff=0) help.add_command(label="About") menubar.add_cascade(label="Help", menu=help) top.config(menu=menubar) top.mainloop() Output:
Next TopicPython Tkinter Message
|