class menu.Menu(Dialog)

The Menu class provides a drop-down command menu for use with a MenuBar. It can also be used on its own as a pop-up menu of selectable items.

Menu item specifications

When creating the menu, items are specified as a list of tuples:

(text, command_name)

where text is the title to be displayed for the item, optionally followed by a slash and a key combination specifier.

A key combination specifier consists of a single printable ASCII character, optionally preceded by one or more of the following modifiers:

CharacterKey
^Shift
@Alt or Option

The command_name is an internal name used to associate the item with handling methods. Two method names are derived from the command name: a handler method (suffixed with '_cmd') and an enabling method (suffixed with '_enabled'). See MenuBar for details of how these methods are used.

An empty tuple may be used to create a separator between groups of menu items.

Example

file_menu = Menu('File', [
    ('Open/O', 'open'),
    (),
    ('Save/S', 'save'),
    (Save As/^S', 'save_as')
])

Using Menus

There are two ways that a Menu can be used: as a drop-down menu in a menu bar, or as a stand-alone pop-up menu.

To use it as a drop-down menu, simply add it to the menus list of a MenuBar. The menu bar takes care of showing and interacting with the menu automatically.

To use it as a pop-up menu, you will need to call the present() method, and possibly the find_item_for_key() and/or invoke_command() methods yourself.

Constructor

Menu(title, items)
Creates a Menu with the specified title and item list. The title is not displayed in the menu itself, but is used when the menu is attached to a MenuBar.

Methods

present(self, client, position)
Displays the menu and allows an item to be selected from it. The top left corner of the menu is placed at the given position relative to the given client widget. Retains control until an item is selected, or a mouse click occurs outside the menu, or the Escape key is pressed. Returns the index of the selected item (0-based), or -1 if no item was selected.
find_item_for_key(event)
Given a key event, finds a matching enabled item and returns its index (0-based). Returns -1 if no matching item is found or the matching item is not enabled.
invoke_item(index)
Locates and calls a command handler for the item with the given index (0-based). Does nothing if the index is -1. Note: Does not check whether the item is enabled.

---