Section 17.5. Menus


17.5. Menus

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

m.add(entry_kind, **entry_options)

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.

delete

m.delete(i[,j])

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.

entryconfigure, entryconfig

m.entryconfigure(i, **entry_options)

Changes entry options for m's i enTRy. entryconfig is an exact synonym.

insert, insert_cascade, insert_checkbutton, insert_command, insert_radiobutton, insert_separator

m.insert(i,entry_kind, **entry_options)

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.

invoke

m.invoke(i)

Invokes m's i entry, just as if the user clicked on it.

post

m.post(x,y)

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).

unpost

m.unpost( )

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:


cascade

A submenu; option menu=x must give as x another Menu instance


checkbutton

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


command

Similar to a Button widget; a typical option is command=callable


radiobutton

Similar to a Radiobutton widget; typical options are variable (to indicate a Tkinter variable object), value, and, optionally, command, like for a Radiobutton instance


separator

A line segment that separates groups of other entries

Other options often used with menu entries are:


image

Option image=x uses x, a Tkinter image object, to label the entry with an image rather than text.


label

Option label=somestring labels the entry with a text string.


underline

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:

 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) root.config(menu=bar) Tkinter.mainloop( ) 

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     return emit 

and use command=mkshow('File', x) and command=mkshow('Edit', x), respectively, in the calls to the add_command methods of fil and edi.




Python in a Nutshell
Python in a Nutshell, Second Edition (In a Nutshell)
ISBN: 0596100469
EAN: 2147483647
Year: 2004
Pages: 192
Authors: Alex Martelli

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net