You are reading the article Syntax And Menu Methods In Tkinter With Examples updated in September 2023 on the website Speedmintonvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Syntax And Menu Methods In Tkinter With Examples
Overview of Tkinter MenuWeb development, programming languages, Software testing & others
There is two popular variety of Menu, we will discuss here:
Pull-down
Pop-up
Syntax:
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 ExamplesThe different methods are given below:
1. Pull-down MenuLet’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:
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.
Then here is the approach:
Code:
from tkinter import * Items = [ "egg", "toffee", "chicken", "beef", "bread", "Choclate", "Milk" ] Master = Tk() wi.pack() mainloop()Output:
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:
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:
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 MenuThe 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.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
Output #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)”
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.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:
ConclusionAs 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 ArticlesThis 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 –
You're reading Syntax And Menu Methods In Tkinter With Examples
Update the detailed information about Syntax And Menu Methods In Tkinter With Examples on the Speedmintonvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!