16.5 MenusClass Menu implements all kinds of menus: menubars of top-level windows, submenus, and pop-up menus. To use a Menu instance m as the menubar 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 a named argument menu=m. To use m as a pop-up menu, call method m.post. Besides configuration options covered in Section 16.2.1 earlier in this chapter, a Menu instance m supports option postcommand=callable. Tkinter calls callable without arguments each time it is about to display m (whether because of a call to m.post or because of user actions). You can 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. 16.5.1 Menu-Specific MethodsBesides methods common to all widgets, an instance m of class Menu supplies several menu-specific methods.
Adds after m's existing entries a new entry whose kind is the string entry_kind, which is one of the strings 'cascade', 'checkbutton', 'command', 'radiobutton', or 'separator'. Section 16.5.2 later in this chapter covers entry kinds and options. Methods whose names start with add_ work just like method add, but they accept no positional argument; what 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 the i one to the j one, included. The first entry has index 0.
Changes entry options for m's i entry. entryconfig is an exact synonym.
Adds before m's entry i a new entry whose kind is the string entry_kind, which is one of the strings 'cascade', 'checkbutton', 'command', 'radiobutton', or 'separator'. Section 16.5.2 later in this chapter covers entry kinds and options. Methods whose names start with insert_ work just like method insert, except that they don't accept a second positional argument; what kind of entry each method 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 upper left corner of Tkinter's root window).
Closes m if m was displaying as a pop-up menu, otherwise does nothing. 16.5.2 Menu EntriesWhen a menu m displays, it shows a vertical (horizontal for a menubar) list of entries. Each entry can be one of the following kinds:
Other entry options often used with menu entries are:
16.5.3 Menu ExampleThe following example shows how to add a menubar with typical File and Edit menus: import Tkinter root = Tkinter.Tk( ) bar = Tkinter.Menu( ) def show(menu, entry): print menu, entry fil = Tkinter.Menu( ) for x in 'New', 'Open', 'Close', 'Save': fil.add_command(label=x,command=lambda x=x:show('File',x)) bar.add_cascade(label='File',menu=fil) edi = Tkinter.Menu( ) for x in 'Cut', 'Copy', 'Paste', 'Clear': edi.add_command(label=x,command=lambda x=x:show('Edit',x)) bar.add_cascade(label='Edit',menu=edi) 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 good alternative to the lambda expressions would be a closure. Instead of def show, use: def mkshow(menu): def emit(entry, menu=menu): print menu, entry return emit and use command=mkshow('File') and command=mkshow('Edit'), respectively, in the calls to the add_command methods of fil and edi. |