The objective of this widget is to authorize us to generate all types of menus that could be utilized by our applications. The center functionality offers a way to make three menu categories: pop-up, top-level, and pull-down. It is also potential to utilize other expanded widgets to realize new forms of menus, such as the OptionMenu widget, which applies a special sort that produces a pop-up list of objects within an assortment.
Here is the simple syntax to create this widget:
w = Menu ( master, option, ... )
The background color will appear on a choice when it is under the mouse.
Specifies the width of a border drawn around a choice when it is under the mouse. Default is 1 pixel.
The foreground color will appear on a choice when it is under the mouse.
The background color for choices is not under the mouse.
The width of the border around all the choices. Default is 1.
The cursor appears when the mouse is over the choices, but only when the menu has been torn off.
The color of the text for items whose state is DISABLED.
The default font for textual choices.
The foreground color is used for choices not under the mouse.
You can set this option to a procedure, and that procedure will be called every time someone brings up this menu.
The default 3-D effect for menus is relief=RAISED.
To display an image on this menubutton.
Specifies the color displayed in checkbuttons and radiobuttons when they are selected.
Normally, a menu can be torn off, the first position (position 0) in the list of choices is occupied by the tear-off element, and the additional choices are added starting at position 1. If you set tearoff=0, the menu will not have a tear-off feature, and choices will be added starting at position 0.
Normally, the title of a tear-off menu window will be the same as the text of the menubutton or cascade that leads to this menu. If you want to change the title of that window, set the title option to that string.
These methods are available on Menu objects -
|Sr.No.||Option & Description|
Adds a menu item to the menu.
add_radiobutton( options )
Creates a radio button menu item.
add_checkbutton( options )
Creates a check button menu item.
Creates a new hierarchical menu by associating a given menu to a parent menu
Adds a separator line to the menu.
add( type, options )
Adds a specific type of menu item to the menu.
delete( startindex [, endindex ])
Deletes the menu items ranging from startindex to endindex.
entryconfig( index, options )
Allows you to modify a menu item, which is identified by the index, and change its options.
Returns the index number of the given menu item label.
insert_separator ( index )
Insert a new separator at the position specified by the index.
invoke ( index )
Calls the command callback associated with the choice at position index. If a checkbutton, its state is toggled between set and cleared; if a radiobutton, that choice is set.
type ( index )
Returns the type of the choice specified by index: either "cascade", "checkbutton", "command", "radiobutton", "separator", or "tearoff".
Try the following example yourself
Web content is composed with the free online HTML editor. Please purchase a membership to remove promotional messages like this one.
from Tkinter import * def donothing(): filewin = Toplevel(root) button = Button(filewin, text="Do nothing button") button.pack() root = Tk() menubar = Menu(root) filemenu = Menu(menubar, tearoff=0) filemenu.add_command(label="New", command=donothing) filemenu.add_command(label="Open", command=donothing) filemenu.add_command(label="Save", command=donothing) filemenu.add_command(label="Save as...", command=donothing) filemenu.add_command(label="Close", command=donothing) filemenu.add_separator() filemenu.add_command(label="Exit", command=root.quit) menubar.add_cascade(label="File", menu=filemenu) editmenu = Menu(menubar, tearoff=0) editmenu.add_command(label="Undo", command=donothing) editmenu.add_separator() editmenu.add_command(label="Cut", command=donothing) editmenu.add_command(label="Copy", command=donothing) editmenu.add_command(label="Paste", command=donothing) editmenu.add_command(label="Delete", command=donothing) editmenu.add_command(label="Select All", command=donothing) menubar.add_cascade(label="Edit", menu=editmenu) helpmenu = Menu(menubar, tearoff=0) helpmenu.add_command(label="Help Index", command=donothing) helpmenu.add_command(label="About...", command=donothing) menubar.add_cascade(label="Help", menu=helpmenu) root.config(menu=menubar) root.mainloop()
When the above code is executed, it produces the following result:
Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.