.. include:: headings.inc .. _Menu: ========================================================================================================================================== |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 :meth:`Menu.IsChecked` method of :ref:`Menu` or :ref:`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 :meth:`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 :ref:`MenuBar` event processing is used. With a popup menu (see :meth:`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 :ref:`Menu` and define event table entries using the ``EVT_MENU`` macro; - Set a new event handler for :ref:`Menu`, through :meth:`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 :ref:`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). .. seealso:: :ref:`MenuBar`, :meth:`Window.PopupMenu` , :ref:`Events and Event Handling `, `FileHistory` | |class_hierarchy| Inheritance Diagram ===================================== Inheritance diagram for class **Menu** .. raw:: html

Inheritance diagram of Menu

| |method_summary| Methods Summary ================================ ================================================================================ ================================================================================ :meth:`~Menu.__init__` Constructs a :ref:`Menu` object. :meth:`~Menu.Append` Adds a menu item. :meth:`~Menu.AppendCheckItem` Adds a checkable item to the end of the menu. :meth:`~Menu.AppendRadioItem` Adds a radio item to the end of the menu. :meth:`~Menu.AppendSeparator` Adds a separator to the end of the menu. :meth:`~Menu.AppendSubMenu` Adds the given `submenu` to this menu. :meth:`~Menu.Attach` :meth:`~Menu.Break` Inserts a break in a menu, causing the next appended item to appear in a new column. :meth:`~Menu.Check` Checks or unchecks the menu item. :meth:`~Menu.Delete` Deletes the menu item from the menu. :meth:`~Menu.DestroyItem` Deletes the menu item from the menu. :meth:`~Menu.Detach` :meth:`~Menu.Enable` Enables or disables (greys out) a menu item. :meth:`~Menu.FindChildItem` Finds the menu item object associated with the given menu item identifier and, optionally, the position of the item in the menu. :meth:`~Menu.FindItem` Finds the menu id for a menu item string. :meth:`~Menu.FindItemByPosition` Returns the :ref:`MenuItem` given a position in the menu. :meth:`~Menu.GetHelpString` Returns the help string associated with a menu item. :meth:`~Menu.GetInvokingWindow` :meth:`~Menu.GetLabel` Returns a menu item label. :meth:`~Menu.GetLabelText` Returns a menu item label, without any of the original mnemonics and accelerators. :meth:`~Menu.GetMenuItemCount` Returns the number of items in the menu. :meth:`~Menu.GetMenuItems` Returns the list of items in the menu. :meth:`~Menu.GetParent` :meth:`~Menu.GetStyle` :meth:`~Menu.GetTitle` Returns the title of the menu. :meth:`~Menu.GetWindow` :meth:`~Menu.Insert` Inserts the given `item` before the position `pos`. :meth:`~Menu.InsertCheckItem` Inserts a checkable item at the given position. :meth:`~Menu.InsertRadioItem` Inserts a radio item at the given position. :meth:`~Menu.InsertSeparator` Inserts a separator at the given position. :meth:`~Menu.IsAttached` :meth:`~Menu.IsChecked` Determines whether a menu item is checked. :meth:`~Menu.IsEnabled` Determines whether a menu item is enabled. :meth:`~Menu.Prepend` Inserts the given `item` at position 0, i.e. :meth:`~Menu.PrependCheckItem` Inserts a checkable item at position 0. :meth:`~Menu.PrependRadioItem` Inserts a radio item at position 0. :meth:`~Menu.PrependSeparator` Inserts a separator at position 0. :meth:`~Menu.Remove` Removes the menu item from the menu but doesn't delete the associated ``C++`` object. :meth:`~Menu.SetHelpString` Sets an item's help string. :meth:`~Menu.SetInvokingWindow` :meth:`~Menu.SetLabel` Sets the label of a menu item. :meth:`~Menu.SetParent` :meth:`~Menu.SetTitle` Sets the title of the menu. :meth:`~Menu.UpdateUI` Sends events to `source` (or owning window if ``None``) to update the menu UI. ================================================================================ ================================================================================ | |property_summary| Properties Summary ===================================== ================================================================================ ================================================================================ :attr:`~Menu.InvokingWindow` See :meth:`~Menu.GetInvokingWindow` and :meth:`~Menu.SetInvokingWindow` :attr:`~Menu.MenuItemCount` See :meth:`~Menu.GetMenuItemCount` :attr:`~Menu.MenuItems` See :meth:`~Menu.GetMenuItems` :attr:`~Menu.Parent` See :meth:`~Menu.GetParent` and :meth:`~Menu.SetParent` :attr:`~Menu.Style` See :meth:`~Menu.GetStyle` :attr:`~Menu.Title` See :meth:`~Menu.GetTitle` and :meth:`~Menu.SetTitle` :attr:`~Menu.Window` See :meth:`~Menu.GetWindow` ================================================================================ ================================================================================ | |api| Class API =============== .. class:: Menu(EvtHandler) 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) .. method:: __init__(self, *args, **kw) |overload| **Overloaded Implementations**: **~~~** **__init__** `(self)` Constructs a :ref:`Menu` object. **~~~** **__init__** `(self, style)` Constructs a :ref:`Menu` object. :param `style`: If set to ``MENU_TEAROFF``, the menu will be detachable (wxGTK only). :type `style`: long **~~~** **__init__** `(self, title, style=0)` Constructs a :ref:`Menu` object with a title. :param `title`: Title at the top of the menu (not always supported). :type `title`: string :param `style`: If set to ``MENU_TEAROFF``, the menu will be detachable (wxGTK only). :type `style`: long **~~~** .. method:: Append(self, *args, **kw) |overload| **Overloaded Implementations**: **~~~** **Append** `(self, id, item='', helpString='', kind=ITEM_NORMAL)` Adds a menu item. :param `id`: The menu command identifier. :type `id`: int :param `item`: The string to appear on the menu item. See :meth:`MenuItem.SetItemLabel` for more details. :type `item`: string :param `helpString`: 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. :type `helpString`: string :param `kind`: May be ``ITEM_SEPARATOR`` , ``ITEM_NORMAL`` , ``ITEM_CHECK`` or ``ITEM_RADIO`` . :type `kind`: ItemKind :rtype: :ref:`MenuItem` :: self.fileMenu.Append(ID_NEW_FILE, "&New file\tCTRL+N", "Creates a XYZ document") or even better for stock menu items (see :meth:`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. .. seealso:: :meth:`AppendSeparator` , :meth:`AppendCheckItem` , :meth:`AppendRadioItem` , :meth:`AppendSubMenu` , :meth:`Insert` , :meth:`SetLabel` , :meth:`GetHelpString` , :meth:`SetHelpString` , :ref:`MenuItem` **~~~** **Append** `(self, id, item, subMenu, helpString='')` Adds a submenu. :param `id`: The menu command identifier. :type `id`: int :param `item`: The string to appear on the menu item. :type `item`: string :param `subMenu`: Pull-right submenu. :type `subMenu`: Menu :param `helpString`: 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. :type `helpString`: string :rtype: :ref:`MenuItem` .. deprecated:: 2.9.4 This function is deprecated, use :meth:`AppendSubMenu` instead. .. seealso:: :meth:`AppendSeparator` , :meth:`AppendCheckItem` , :meth:`AppendRadioItem` , :meth:`AppendSubMenu` , :meth:`Insert` , :meth:`SetLabel` , :meth:`GetHelpString` , :meth:`SetHelpString` , :ref:`MenuItem` **~~~** **Append** `(self, menuItem)` Adds a menu item object. This is the most generic variant of :meth:`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. :param `menuItem`: A menuitem object. It will be owned by the :ref:`Menu` object after this function is called, so do not delete it yourself. :type `menuItem`: MenuItem :rtype: :ref:`MenuItem` .. note:: See the remarks for the other :meth:`Append` overloads. .. seealso:: :meth:`AppendSeparator` , :meth:`AppendCheckItem` , :meth:`AppendRadioItem` , :meth:`AppendSubMenu` , :meth:`Insert` , :meth:`SetLabel` , :meth:`GetHelpString` , :meth:`SetHelpString` , :ref:`MenuItem` **~~~** .. method:: AppendCheckItem(self, id, item, help='') Adds a checkable item to the end of the menu. :param `id`: :type `id`: int :param `item`: :type `item`: string :param `help`: :type `help`: string :rtype: :ref:`MenuItem` .. seealso:: :meth:`Append` , :meth:`InsertCheckItem` .. method:: AppendRadioItem(self, id, item, help='') 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. :param `id`: :type `id`: int :param `item`: :type `item`: string :param `help`: :type `help`: string :rtype: :ref:`MenuItem` .. note:: Radio items are not supported under Motif. .. seealso:: :meth:`Append` , :meth:`InsertRadioItem` .. method:: AppendSeparator(self) Adds a separator to the end of the menu. :rtype: :ref:`MenuItem` .. seealso:: :meth:`Append` , :meth:`InsertSeparator` .. method:: AppendSubMenu(self, submenu, text, help='') 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. :param `submenu`: :type `submenu`: Menu :param `text`: :type `text`: string :param `help`: :type `help`: string :rtype: :ref:`MenuItem` .. method:: Attach(self, menubar) :param `menubar`: :type `menubar`: MenuBar .. method:: Break(self) Inserts a break in a menu, causing the next appended item to appear in a new column. .. method:: Check(self, id, check) Checks or unchecks the menu item. :param `id`: The menu item identifier. :type `id`: int :param `check`: If ``True``, the item will be checked, otherwise it will be unchecked. :type `check`: bool .. seealso:: :meth:`IsChecked` .. method:: Delete(self, *args, **kw) |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 :meth:`Destroy` if you want to delete a submenu. :param `id`: Id of the menu item to be deleted. :type `id`: int :rtype: `bool` .. seealso:: :meth:`FindItem` , :meth:`Destroy` , :meth:`Remove` **~~~** **Delete** `(self, item)` Deletes the menu item from the menu. If the item is a submenu, it will **not** be deleted. Use :meth:`Destroy` if you want to delete a submenu. :param `item`: Menu item to be deleted. :type `item`: MenuItem :rtype: `bool` .. seealso:: :meth:`FindItem` , :meth:`Destroy` , :meth:`Remove` **~~~** .. method:: DestroyItem(self, *args, **kw) |overload| **Overloaded Implementations**: **~~~** **DestroyItem** `(self, id)` Deletes the menu item from the menu. If the item is a submenu, it will be deleted. Use :meth:`Remove` if you want to keep the submenu (for example, to reuse it later). :param `id`: Id of the menu item to be deleted. :type `id`: int :rtype: `bool` .. seealso:: :meth:`FindItem` , :meth:`Delete` , :meth:`Remove` **~~~** **DestroyItem** `(self, item)` Deletes the menu item from the menu. If the item is a submenu, it will be deleted. Use :meth:`Remove` if you want to keep the submenu (for example, to reuse it later). :param `item`: Menu item to be deleted. :type `item`: MenuItem :rtype: `bool` .. seealso:: :meth:`FindItem` , :meth:`Delete` , :meth:`Remove` **~~~** .. method:: Detach(self) .. method:: Enable(self, id, enable) Enables or disables (greys out) a menu item. :param `id`: The menu item identifier. :type `id`: int :param `enable`: ``True`` to enable the menu item, ``False`` to disable it. :type `enable`: bool .. seealso:: :meth:`IsEnabled` .. method:: FindChildItem(self, id, pos=None) Finds the menu item object associated with the given menu item identifier and, optionally, the position of the item in the menu. Unlike :meth:`FindItem` , this function doesn't recurse but only looks at the direct children of this menu. :param `id`: The identifier of the menu item to find. :type `id`: int :param `pos`: If the pointer is not ``None``, it is filled with the item's position if it was found or :type `pos`: int :rtype: :ref:`MenuItem` :returns: Menu item object or ``None`` if not found. .. method:: FindItem(self, *args, **kw) |overload| **Overloaded Implementations**: **~~~** **FindItem** `(self, itemString)` Finds the menu id for a menu item string. :param `itemString`: Menu item string to find. :type `itemString`: string :rtype: `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. :param `id`: Menu item identifier. :type `id`: int :param `menu`: If the pointer is not ``None``, it will be filled with the item's parent menu (if the item was found) :type `menu`: Menu :rtype: :ref:`MenuItem` :returns: Menu item object or ``None`` if none is found. **~~~** .. method:: FindItemByPosition(self, position) Returns the :ref:`MenuItem` given a position in the menu. :param `position`: :type `position`: int :rtype: :ref:`MenuItem` .. method:: GetHelpString(self, id) Returns the help string associated with a menu item. :param `id`: The menu item identifier. :type `id`: int :rtype: `string` :returns: The help string, or the empty string if there is no help string or the item was not found. .. seealso:: :meth:`SetHelpString` , :meth:`Append` .. method:: GetInvokingWindow(self) :rtype: :ref:`Window` .. method:: GetLabel(self, id) Returns a menu item label. :param `id`: The menu item identifier. :type `id`: int :rtype: `string` :returns: The item label, or the empty string if the item was not found. .. seealso:: :meth:`GetLabelText` , :meth:`SetLabel` .. method:: GetLabelText(self, id) Returns a menu item label, without any of the original mnemonics and accelerators. :param `id`: The menu item identifier. :type `id`: int :rtype: `string` :returns: The item label, or the empty string if the item was not found. .. seealso:: :meth:`GetLabel` , :meth:`SetLabel` .. method:: GetMenuItemCount(self) Returns the number of items in the menu. :rtype: `int` .. method:: GetMenuItems(self, *args, **kw) Returns the list of items in the menu. MenuItemList is a pseudo-template list class containing :ref:`MenuItem` pointers, see List. .. method:: GetParent(self) :rtype: :ref:`Menu` .. method:: GetStyle(self) :rtype: `long` .. method:: GetTitle(self) Returns the title of the menu. :rtype: `string` .. seealso:: :meth:`SetTitle` .. method:: GetWindow(self) :rtype: :ref:`Window` .. method:: Insert(self, *args, **kw) Inserts the given `item` before the position `pos`. Inserting the item at position :meth:`GetMenuItemCount` is the same as appending it. .. seealso:: :meth:`Append` , :meth:`Prepend` |overload| **Overloaded Implementations**: **~~~** **Insert** `(self, pos, menuItem)` :param `pos`: :type `pos`: int :param `menuItem`: :type `menuItem`: MenuItem :rtype: :ref:`MenuItem` **~~~** **Insert** `(self, pos, id, item='', helpString='', kind=ITEM_NORMAL)` :param `pos`: :type `pos`: int :param `id`: :type `id`: int :param `item`: :type `item`: string :param `helpString`: :type `helpString`: string :param `kind`: :type `kind`: ItemKind :rtype: :ref:`MenuItem` **~~~** .. method:: InsertCheckItem(self, pos, id, item, helpString='') Inserts a checkable item at the given position. :param `pos`: :type `pos`: int :param `id`: :type `id`: int :param `item`: :type `item`: string :param `helpString`: :type `helpString`: string :rtype: :ref:`MenuItem` .. seealso:: :meth:`Insert` , :meth:`AppendCheckItem` .. method:: InsertRadioItem(self, pos, id, item, helpString='') Inserts a radio item at the given position. :param `pos`: :type `pos`: int :param `id`: :type `id`: int :param `item`: :type `item`: string :param `helpString`: :type `helpString`: string :rtype: :ref:`MenuItem` .. seealso:: :meth:`Insert` , :meth:`AppendRadioItem` .. method:: InsertSeparator(self, pos) Inserts a separator at the given position. :param `pos`: :type `pos`: int :rtype: :ref:`MenuItem` .. seealso:: :meth:`Insert` , :meth:`AppendSeparator` .. method:: IsAttached(self) :rtype: `bool` .. method:: IsChecked(self, id) Determines whether a menu item is checked. :param `id`: The menu item identifier. :type `id`: int :rtype: `bool` :returns: ``True`` if the menu item is checked, ``False`` otherwise. .. seealso:: :meth:`Check` .. method:: IsEnabled(self, id) Determines whether a menu item is enabled. :param `id`: The menu item identifier. :type `id`: int :rtype: `bool` :returns: ``True`` if the menu item is enabled, ``False`` otherwise. .. seealso:: :meth:`Enable` .. method:: Prepend(self, *args, **kw) Inserts the given `item` at position 0, i.e. before all the other existing items. .. seealso:: :meth:`Append` , :meth:`Insert` |overload| **Overloaded Implementations**: **~~~** **Prepend** `(self, item)` :param `item`: :type `item`: MenuItem :rtype: :ref:`MenuItem` **~~~** **Prepend** `(self, id, item='', helpString='', kind=ITEM_NORMAL)` :param `id`: :type `id`: int :param `item`: :type `item`: string :param `helpString`: :type `helpString`: string :param `kind`: :type `kind`: ItemKind :rtype: :ref:`MenuItem` **~~~** .. method:: PrependCheckItem(self, id, item, helpString='') Inserts a checkable item at position 0. :param `id`: :type `id`: int :param `item`: :type `item`: string :param `helpString`: :type `helpString`: string :rtype: :ref:`MenuItem` .. seealso:: :meth:`Prepend` , :meth:`AppendCheckItem` .. method:: PrependRadioItem(self, id, item, helpString='') Inserts a radio item at position 0. :param `id`: :type `id`: int :param `item`: :type `item`: string :param `helpString`: :type `helpString`: string :rtype: :ref:`MenuItem` .. seealso:: :meth:`Prepend` , :meth:`AppendRadioItem` .. method:: PrependSeparator(self) Inserts a separator at position 0. :rtype: :ref:`MenuItem` .. seealso:: :meth:`Prepend` , :meth:`AppendSeparator` .. method:: Remove(self, *args, **kw) |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). :param `id`: The identifier of the menu item to remove. :type `id`: int :rtype: :ref:`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). :param `item`: The menu item to remove. :type `item`: MenuItem :rtype: :ref:`MenuItem` :returns: A pointer to the item which was detached from the menu. **~~~** .. method:: SetHelpString(self, id, helpString) Sets an item's help string. :param `id`: The menu item identifier. :type `id`: int :param `helpString`: The help string to set. :type `helpString`: string .. seealso:: :meth:`GetHelpString` .. method:: SetInvokingWindow(self, win) :param `win`: :type `win`: Window .. method:: SetLabel(self, id, label) Sets the label of a menu item. :param `id`: The menu item identifier. :type `id`: int :param `label`: The menu item label to set. :type `label`: string .. seealso:: :meth:`Append` , :meth:`GetLabel` .. method:: SetParent(self, parent) :param `parent`: :type `parent`: Menu .. method:: SetTitle(self, title) Sets the title of the menu. :param `title`: The title to set. :type `title`: string .. 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 :meth:`MenuBar.SetLabelTop` . .. seealso:: :meth:`GetTitle` .. method:: UpdateUI(self, source=None) 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 :meth:`Window.PopupMenu` , but the application may call it at other times if required. :param `source`: :type `source`: EvtHandler .. attribute:: InvokingWindow See :meth:`~Menu.GetInvokingWindow` and :meth:`~Menu.SetInvokingWindow` .. attribute:: MenuItemCount See :meth:`~Menu.GetMenuItemCount` .. attribute:: MenuItems See :meth:`~Menu.GetMenuItems` .. attribute:: Parent See :meth:`~Menu.GetParent` and :meth:`~Menu.SetParent` .. attribute:: Style See :meth:`~Menu.GetStyle` .. attribute:: Title See :meth:`~Menu.GetTitle` and :meth:`~Menu.SetTitle` .. attribute:: Window See :meth:`~Menu.GetWindow`