Class Menu implements all kinds of menus:
menu bars of top-level windows, submenus, and pop-up menus. To use a Menu instance m as the menu bar for a top-level window w, set w's configuration option menu=m. To use m as a submenu of a Menu instance x, call x.add_cascade with menu=m. To use m as a pop-up menu, call m.post.
Besides configuration options covered in "Common Widget Options" on page 409, a Menu instance m supports option postcommand=callable. Tkinter calls callable without arguments each time it is about to display m (because of a call to m.post or because of user actions). Use this option to update a dynamic menu just in time when necessary.
By default, a Tkinter menu shows a tear-off entry (a dashed line before other entries), which lets the user get a copy of the menu in a separate Toplevel window. Since such tear-offs are not part of user interface standards on popular platforms, you may want to disable tear-off functionality by using configuration option tearoff=0 for the menu.
17.5.1. Menu-Specific Methods
Besides methods common to all widgets, an instance m of class Menu supplies several menu-specific methods.
add, add_cascade, add_checkbutton, add_command, add_radiobutton, add_separator
Adds after m's existing entries a new entry whose kind is the string entry_kind, one of 'cascade', 'checkbutton', 'command', 'radiobutton', or 'separator'. "Menu Entries" on page 425 covers entry kinds and options.
Methods whose names start with add_ work like method add, but accept no positional argument; the kind of entry each method adds is implied by the method's name.
m.delete(i) removes m's i enTRy. m.delete(i,j) removes m's entries from i to j, included. The first entry has index 0.
Changes entry options for m's i enTRy. entryconfig is an exact synonym.
insert, insert_cascade, insert_checkbutton, insert_command, insert_radiobutton, insert_separator
Adds before m's entry i a new entry whose kind is the string entry_kind, one of 'cascade', 'checkbutton', 'command', 'radiobutton', or 'separator'. "Menu Entries" on page 425 covers entry kinds and options.
Methods whose names start with insert_ work just like method insert, without a second argument; the kind of entry each inserts is implied by the method's name.
Invokes m's i entry, just as if the user clicked on it.
Displays m as a pop-up menu, with m's upper-left corner at coordinates x, y (offsets in pixels from the upper-left corner of Tkinter's root window).
Closes m if m was displaying as a pop-up menu; otherwise, does nothing.
17.5.2. Menu Entries
When a menu m displays, it shows a vertical (horizontal for a menu bar) list of entries. Each entry can be one of the following kinds:
A submenu; option menu=x must give as x another Menu instance
Similar to a Checkbutton widget; typical options are variable (to indicate a Tkinter variable object), onvalue, offvalue, and, optionally, command, like for a Checkbutton instance
Similar to a Button widget; a typical option is command=callable
Similar to a Radiobutton widget; typical options are variable (to indicate a Tkinter variable object), value, and, optionally, command, like for a Radiobutton instance
A line segment that separates groups of other entries
Other options often used with menu entries are:
Option image=x uses x, a Tkinter image object, to label the entry with an image rather than text.
Option label=somestring labels the entry with a text string.
Option underline=x gives x as the index of the character to underline within the entry's label (0 is the first character, 1 the second one, and so on).
17.5.3. Menu Example
The following example shows how to add a menu bar with typical File and Edit menus:
root = Tkinter.Tk( )
bar = Tkinter.Menu( )
def show(menu, entry): print menu, entry
fil = Tkinter.Menu( )
for x in 'New', 'Open', 'Close', 'Save':
edi = Tkinter.Menu( )
for x in 'Cut', 'Copy', 'Paste', 'Clear':
In this example, each menu command just outputs information to standard output for demonstration purposes. (Note the x=x idiom to snapshot the value of x at the time we create each lambda.) Otherwise, the current value of x at the time a lambda executes, 'Clear', would show up at each menu selection. A better alternative to the lambda expressions with the x=x idiom is a closure. Instead of def show, use:
def mkshow(menu, entry):
def emit( ): print menu, entry
and use command=mkshow('File', x) and command=mkshow('Edit', x), respectively, in the calls to the add_command methods of fil and edi.