Tkinter Menu options

So which options does tk.Menu has. I found only master and tearoff. Is that all?

Possibly, this will help you. It’s directly from my own notes:

Menu

Syntax

menu = Menu(master, options)

  • master: This parameter is used to represents the parent window.

  • options: There are many options which are available and they can be used as key-value pairs separated by commas.

Note: There’s no list of options, so fill this in, using the same format as above, as you learn.

Example:

# importing only those functions which are needed
from tkinter import*
from tkinter.ttk import*
from time import strftime

# creating tkinter window
root = Tk()
root.title('Menu Demonstration')

# Creating Menubar
menubar = Menu(root)

# Adding File Menu and commands
file = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='File', menu = file)
file.add_command(label ='New File', command = None)
file.add_command(label ='Open...', command = None)
file.add_command(label ='Save', command = None)
file.add_separator()
file.add_command(label ='Exit', command = root.destroy)

# Adding Edit Menu and commands
edit = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Edit', menu = edit)
edit.add_command(label ='Cut', command = None)
edit.add_command(label ='Copy', command = None)
edit.add_command(label ='Paste', command = None)
edit.add_command(label ='Select All', command = None)
edit.add_separator()
edit.add_command(label ='Find...', command = None)
edit.add_command(label ='Find again', command = None)

# Adding Help Menu
help_ = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Help', menu = help_)
help_.add_command(label ='Tk Help', command = None)
help_.add_command(label ='Demo', command = None)
help_.add_separator()
help_.add_command(label ='About Tk', command = None)

# display Menu
root.config(menu = menubar)
mainloop()

In above example, commands are set to None (with the exception of Exit) but one may add different commands to different labels to perform the required task.

Tkinter is an interface to the Tk GUI library, so for documentation on what options are available you’ll want to go to those docs. Here’s the menu docs, for example. The commands described there generally correspond exactly to a Tkinter method.

1 Like

Hi,

the example that Rob provided is actually a very good example. As per the syntax, and subsequent example, the master argument is mandatory as that represents the window that it is being ‘appended’ to. The options argument(s) is/are user/developer dependent as the sample code clearly demonstrates. That is, you get to decide how many options are listed per each menu heading. The command option references the method to be called during each instance that a particular sub-menu is selected (i.e., actions to be executed). In the sample code provided, none were given I take it for simplicity and tutorial purposes. In actual code, there would be an associated method stated for action to be taken (Save a file, delete a file, etc.).

In a way, you can say that the options arguments are open ended since this is where the developer designs the menu as per the design requirements. Also, you as the developer decides the names of the menu headings and sub-menu names. You don’t have to follow the names stated in the example. However, the ones given are pretty common.

1 Like