Table Of Contents

Previous topic

MemoryFSHandler

Next topic

MenuBar

This Page

phoenix_title Menu

A menu is a popup (or pull down) list of items, one of which may be selected before the menu goes away (clicking elsewhere dismisses the menu).

Menus may be used to construct either menu bars or popup menus.

A menu item has an integer ID associated with it which can be used to identify the selection, or to change the menu item in some way. A menu item with a special identifier `ID_SEPARATOR` is a separator item and doesn’t have an associated command but just makes a separator line appear in the menu.

Menu items may be either normal items, check items or radio items. Normal items don’t have any special properties while the check items have a boolean flag associated to them and they show a checkmark in the menu when the flag is set. wxWidgets automatically toggles the flag value when the item is clicked and its value may be retrieved using either Menu.IsChecked method of Menu or MenuBar itself or by using Event.IsChecked when you get the menu notification for the item in question.

The radio items are similar to the check items except that all the other items in the same radio group are unchecked when a radio item is checked. The radio group is formed by a contiguous range of radio items, i.e. it starts at the first item of this kind and ends with the first item of a different kind (or the end of the menu). Notice that because the radio groups are defined in terms of the item positions inserting or removing the items in the menu containing the radio items risks to not work correctly.

phoenix_title Allocation strategy

All menus must be created on the heap because all menus attached to a menubar or to another menu will be deleted by their parent when it is deleted. The only exception to this rule are the popup menus (i.e. menus used with Window.PopupMenu ) as wxWidgets does not destroy them to allow reusing the same menu more than once. But the exception applies only to the menus themselves and not to any submenus of popup menus which are still destroyed by wxWidgets as usual and so must be heap-allocated. As the frame menubar is deleted by the frame itself, it means that normally all menus used are deleted automatically.

phoenix_title Event handling

If the menu is part of a menubar, then MenuBar event processing is used. With a popup menu (see Window.PopupMenu ), there is a variety of ways to handle a menu selection event ( wxEVT_COMMAND_MENU_SELECTED ):

  • Provide EVT_MENU handlers in the window which pops up the menu, or in an ancestor of that window (the simplest method);
  • Derive a new class from Menu and define event table entries using the EVT_MENU macro;
  • Set a new event handler for Menu, through EvtHandler.SetNextHandler , specifying an object whose class has EVT_MENU entries;

Note that instead of static EVT_MENU macros you can also use dynamic connection; see Dynamic Event Handling.

Note

Please note that `ID_ABOUT` and `ID_EXIT` are predefined by wxWidgets and have a special meaning since entries using these IDs will be taken out of the normal menus under MacOS X and will be inserted into the system menu (following the appropriate MacOS X interface guideline).


class_hierarchy Inheritance Diagram

Inheritance diagram for class Menu

Inheritance diagram of Menu


method_summary Methods Summary

__init__ Constructs a Menu object.
Append Adds a menu item.
AppendCheckItem Adds a checkable item to the end of the menu.
AppendRadioItem Adds a radio item to the end of the menu.
AppendSeparator Adds a separator to the end of the menu.
AppendSubMenu Adds the given submenu to this menu.
Attach  
Break Inserts a break in a menu, causing the next appended item to appear in a new column.
Check Checks or unchecks the menu item.
Delete Deletes the menu item from the menu.
DestroyItem Deletes the menu item from the menu.
Detach  
Enable Enables or disables (greys out) a menu item.
FindChildItem Finds the menu item object associated with the given menu item identifier and, optionally, the position of the item in the menu.
FindItem Finds the menu id for a menu item string.
FindItemByPosition Returns the MenuItem given a position in the menu.
GetHelpString Returns the help string associated with a menu item.
GetInvokingWindow  
GetLabel Returns a menu item label.
GetLabelText Returns a menu item label, without any of the original mnemonics and accelerators.
GetMenuItemCount Returns the number of items in the menu.
GetMenuItems Returns the list of items in the menu.
GetParent  
GetStyle  
GetTitle Returns the title of the menu.
GetWindow  
Insert Inserts the given item before the position pos.
InsertCheckItem Inserts a checkable item at the given position.
InsertRadioItem Inserts a radio item at the given position.
InsertSeparator Inserts a separator at the given position.
IsAttached  
IsChecked Determines whether a menu item is checked.
IsEnabled Determines whether a menu item is enabled.
Prepend Inserts the given item at position 0, i.e.
PrependCheckItem Inserts a checkable item at position 0.
PrependRadioItem Inserts a radio item at position 0.
PrependSeparator Inserts a separator at position 0.
Remove Removes the menu item from the menu but doesn’t delete the associated C++ object.
SetHelpString Sets an item’s help string.
SetInvokingWindow  
SetLabel Sets the label of a menu item.
SetParent  
SetTitle Sets the title of the menu.
UpdateUI Sends events to source (or owning window if None) to update the menu UI.

api Class API



A menu is a popup (or pull down) list of items, one of which may be selected before the menu goes away (clicking elsewhere dismisses the menu).

Possible constructors:

Menu()

Menu(style)

Menu(title, style=0)

Methods




overload Overloaded Implementations:



__init__ (self)

Constructs a Menu object.



__init__ (self, style)

Constructs a Menu object.

Parameters:style (long) – If set to MENU_TEAROFF, the menu will be detachable (wxGTK only).



__init__ (self, title, style=0)

Constructs a Menu object with a title.

Parameters:
  • title (string) – Title at the top of the menu (not always supported).
  • style (long) – If set to MENU_TEAROFF, the menu will be detachable (wxGTK only).






overload Overloaded Implementations:



Append (self, id, item=’‘, helpString=’‘, kind=ITEM_NORMAL)

Adds a menu item.

Parameters:
  • id (int) – The menu command identifier.
  • item (string) – The string to appear on the menu item. See MenuItem.SetItemLabel for more details.
  • helpString (string) – An optional help string associated with the item. By default, the handler for the wxEVT_MENU_HIGHLIGHT event displays this string in the status line.
  • kind (ItemKind) – May be ITEM_SEPARATOR , ITEM_NORMAL , ITEM_CHECK or ITEM_RADIO .
Return type:

MenuItem

self.fileMenu.Append(ID_NEW_FILE, "&New file\tCTRL+N", "Creates a XYZ document")

or even better for stock menu items (see MenuItem.__init__ ):

self.fileMenu.Append(wx.ID_NEW, "", "Creates a XYZ document")

Note

This command can be used after the menu has been shown, as well as on initial creation of a menu or menubar.



Append (self, id, item, subMenu, helpString=’‘)

Adds a submenu.

Parameters:
  • id (int) – The menu command identifier.
  • item (string) – The string to appear on the menu item.
  • subMenu (Menu) – Pull-right submenu.
  • helpString (string) – An optional help string associated with the item. By default, the handler for the wxEVT_MENU_HIGHLIGHT event displays this string in the status line.
Return type:

MenuItem

Deprecated since version 2.9.4: This function is deprecated, use AppendSubMenu instead.



Append (self, menuItem)

Adds a menu item object.

This is the most generic variant of Append method because it may be used for both items (including separators) and submenus and because you can also specify various extra properties of a menu item this way, such as bitmaps and fonts.

Parameters:menuItem (MenuItem) – A menuitem object. It will be owned by the Menu object after this function is called, so do not delete it yourself.
Return type: MenuItem

Note

See the remarks for the other Append overloads.





Adds a checkable item to the end of the menu.

Parameters:
  • id (int) –
  • item (string) –
  • help (string) –
Return type:

MenuItem

See also

Append , InsertCheckItem



Adds a radio item to the end of the menu.

All consequent radio items form a group and when an item in the group is checked, all the others are automatically unchecked.

Parameters:
  • id (int) –
  • item (string) –
  • help (string) –
Return type:

MenuItem

Note

Radio items are not supported under Motif.

See also

Append , InsertRadioItem



Adds a separator to the end of the menu.

Return type: MenuItem

See also

Append , InsertSeparator



Adds the given submenu to this menu.

text is the text shown in the menu for it and help is the help string shown in the status bar when the submenu item is selected.

Parameters:
  • submenu (Menu) –
  • text (string) –
  • help (string) –
Return type:

MenuItem



Parameters:menubar (MenuBar) –


Inserts a break in a menu, causing the next appended item to appear in a new column.



Checks or unchecks the menu item.

Parameters:
  • id (int) – The menu item identifier.
  • check (bool) – If True, the item will be checked, otherwise it will be unchecked.

See also

IsChecked




overload Overloaded Implementations:



Delete (self, id)

Deletes the menu item from the menu.

If the item is a submenu, it will not be deleted. Use Destroy if you want to delete a submenu.

Parameters:id (int) – Id of the menu item to be deleted.
Return type:bool

See also

FindItem , Destroy , Remove



Delete (self, item)

Deletes the menu item from the menu.

If the item is a submenu, it will not be deleted. Use Destroy if you want to delete a submenu.

Parameters:item (MenuItem) – Menu item to be deleted.
Return type:bool

See also

FindItem , Destroy , Remove






overload Overloaded Implementations:



DestroyItem (self, id)

Deletes the menu item from the menu.

If the item is a submenu, it will be deleted. Use Remove if you want to keep the submenu (for example, to reuse it later).

Parameters:id (int) – Id of the menu item to be deleted.
Return type:bool

See also

FindItem , Delete , Remove



DestroyItem (self, item)

Deletes the menu item from the menu.

If the item is a submenu, it will be deleted. Use Remove if you want to keep the submenu (for example, to reuse it later).

Parameters:item (MenuItem) – Menu item to be deleted.
Return type:bool

See also

FindItem , Delete , Remove







Enables or disables (greys out) a menu item.

Parameters:
  • id (int) – The menu item identifier.
  • enable (bool) – True to enable the menu item, False to disable it.

See also

IsEnabled



Finds the menu item object associated with the given menu item identifier and, optionally, the position of the item in the menu.

Unlike FindItem , this function doesn’t recurse but only looks at the direct children of this menu.

Parameters:
  • id (int) – The identifier of the menu item to find.
  • pos (int) – If the pointer is not None, it is filled with the item’s position if it was found or
Return type:

MenuItem

Returns:

Menu item object or None if not found.




overload Overloaded Implementations:



FindItem (self, itemString)

Finds the menu id for a menu item string.

Parameters:itemString (string) – Menu item string to find.
Return type:int
Returns:Menu item identifier, or NOT_FOUND if none is found.

Note

Any special menu codes are stripped out of source and target strings before matching.



FindItem (self, id, menu=None)

Finds the menu item object associated with the given menu item identifier and, optionally, the (sub)menu it belongs to.

Parameters:
  • id (int) – Menu item identifier.
  • menu (Menu) – If the pointer is not None, it will be filled with the item’s parent menu (if the item was found)
Return type:

MenuItem

Returns:

Menu item object or None if none is found.





Returns the MenuItem given a position in the menu.

Parameters:position (int) –
Return type: MenuItem


Returns the help string associated with a menu item.

Parameters:id (int) – The menu item identifier.
Return type:string
Returns:The help string, or the empty string if there is no help string or the item was not found.

See also

SetHelpString , Append



Return type: Window


Returns a menu item label.

Parameters:id (int) – The menu item identifier.
Return type:string
Returns:The item label, or the empty string if the item was not found.

See also

GetLabelText , SetLabel



Returns a menu item label, without any of the original mnemonics and accelerators.

Parameters:id (int) – The menu item identifier.
Return type:string
Returns:The item label, or the empty string if the item was not found.

See also

GetLabel , SetLabel



Returns the number of items in the menu.

Return type:int


Returns the list of items in the menu.

MenuItemList is a pseudo-template list class containing MenuItem pointers, see List.



Return type: Menu


Return type:long


Returns the title of the menu.

Return type:string

See also

SetTitle



Return type: Window


Inserts the given item before the position pos.

Inserting the item at position GetMenuItemCount is the same as appending it.

See also

Append , Prepend


overload Overloaded Implementations:



Insert (self, pos, menuItem)

Parameters:
Return type:

MenuItem



Insert (self, pos, id, item=’‘, helpString=’‘, kind=ITEM_NORMAL)

Parameters:
  • pos (int) –
  • id (int) –
  • item (string) –
  • helpString (string) –
  • kind (ItemKind) –
Return type:

MenuItem





Inserts a checkable item at the given position.

Parameters:
  • pos (int) –
  • id (int) –
  • item (string) –
  • helpString (string) –
Return type:

MenuItem

See also

Insert , AppendCheckItem



Inserts a radio item at the given position.

Parameters:
  • pos (int) –
  • id (int) –
  • item (string) –
  • helpString (string) –
Return type:

MenuItem

See also

Insert , AppendRadioItem



Inserts a separator at the given position.

Parameters:pos (int) –
Return type: MenuItem

See also

Insert , AppendSeparator



Return type:bool


Determines whether a menu item is checked.

Parameters:id (int) – The menu item identifier.
Return type:bool
Returns:True if the menu item is checked, False otherwise.

See also

Check



Determines whether a menu item is enabled.

Parameters:id (int) – The menu item identifier.
Return type:bool
Returns:True if the menu item is enabled, False otherwise.

See also

Enable



Inserts the given item at position 0, i.e.

before all the other existing items.

See also

Append , Insert


overload Overloaded Implementations:



Prepend (self, item)

Parameters:item (MenuItem) –
Return type: MenuItem



Prepend (self, id, item=’‘, helpString=’‘, kind=ITEM_NORMAL)

Parameters:
  • id (int) –
  • item (string) –
  • helpString (string) –
  • kind (ItemKind) –
Return type:

MenuItem





Inserts a checkable item at position 0.

Parameters:
  • id (int) –
  • item (string) –
  • helpString (string) –
Return type:

MenuItem



Inserts a radio item at position 0.

Parameters:
  • id (int) –
  • item (string) –
  • helpString (string) –
Return type:

MenuItem



Inserts a separator at position 0.

Return type: MenuItem



overload Overloaded Implementations:



Remove (self, id)

Removes the menu item from the menu but doesn’t delete the associated C++ object.

This allows you to reuse the same item later by adding it back to the menu (especially useful with submenus).

Parameters:id (int) – The identifier of the menu item to remove.
Return type: MenuItem
Returns:A pointer to the item which was detached from the menu.



Remove (self, item)

Removes the menu item from the menu but doesn’t delete the associated C++ object.

This allows you to reuse the same item later by adding it back to the menu (especially useful with submenus).

Parameters:item (MenuItem) – The menu item to remove.
Return type: MenuItem
Returns:A pointer to the item which was detached from the menu.





Sets an item’s help string.

Parameters:
  • id (int) – The menu item identifier.
  • helpString (string) – The help string to set.

See also

GetHelpString



Parameters:win (Window) –


Sets the label of a menu item.

Parameters:
  • id (int) – The menu item identifier.
  • label (string) – The menu item label to set.

See also

Append , GetLabel



Parameters:parent (Menu) –


Sets the title of the menu.

Parameters:title (string) – The title to set.

Note

Notice that you can only call this method directly for the popup menus, to change the title of a menu that is part of a menu bar you need to use MenuBar.SetLabelTop .

See also

GetTitle



Sends events to source (or owning window if None) to update the menu UI.

This is called just before the menu is popped up with Window.PopupMenu , but the application may call it at other times if required.

Parameters:source (EvtHandler) –

Properties



See GetInvokingWindow and SetInvokingWindow



See GetMenuItemCount



See GetMenuItems



See GetParent and SetParent



See GetStyle



See GetTitle and SetTitle



See GetWindow