:right-sidebar: True MenuItem =================================================================== .. currentmodule:: gi.repository.Gio .. versionadded:: 2.32 .. class:: MenuItem(**properties: ~typing.Any) :no-contents-entry: Superclasses: :class:`~gi.repository.GObject.Object` :obj:`~gi.repository.Gio.MenuItem` is an opaque structure type. You must access it using the functions below. Constructors ------------ .. rst-class:: interim-class .. class:: MenuItem :no-index: .. classmethod:: new(label: str | None = None, detailed_action: str | None = None) -> ~gi.repository.Gio.MenuItem Creates a new :obj:`~gi.repository.Gio.MenuItem`. If ``label`` is non-:const:`None` it is used to set the "label" attribute of the new item. If ``detailed_action`` is non-:const:`None` it is used to set the "action" and possibly the "target" attribute of the new item. See :func:`~gi.repository.Gio.MenuItem.set_detailed_action` for more information. .. versionadded:: 2.32 :param label: the section label, or :const:`None` :param detailed_action: the detailed action string, or :const:`None` .. classmethod:: new_from_model(model: ~gi.repository.Gio.MenuModel, item_index: int) -> ~gi.repository.Gio.MenuItem Creates a :obj:`~gi.repository.Gio.MenuItem` as an exact copy of an existing menu item in a :obj:`~gi.repository.Gio.MenuModel`. ``item_index`` must be valid (ie: be sure to call :func:`~gi.repository.Gio.MenuModel.get_n_items` first). .. versionadded:: 2.34 :param model: a :obj:`~gi.repository.Gio.MenuModel` :param item_index: the index of an item in ``model`` .. classmethod:: new_section(label: str | None, section: ~gi.repository.Gio.MenuModel) -> ~gi.repository.Gio.MenuItem Creates a new :obj:`~gi.repository.Gio.MenuItem` representing a section. This is a convenience API around :func:`~gi.repository.Gio.MenuItem.new` and :func:`~gi.repository.Gio.MenuItem.set_section`. The effect of having one menu appear as a section of another is exactly as it sounds: the items from ``section`` become a direct part of the menu that ``menu_item`` is added to. Visual separation is typically displayed between two non-empty sections. If ``label`` is non-:const:`None` then it will be encorporated into this visual indication. This allows for labeled subsections of a menu. As a simple example, consider a typical "Edit" menu from a simple program. It probably contains an "Undo" and "Redo" item, followed by a separator, followed by "Cut", "Copy" and "Paste". This would be accomplished by creating three :obj:`~gi.repository.Gio.Menu` instances. The first would be populated with the "Undo" and "Redo" items, and the second with the "Cut", "Copy" and "Paste" items. The first and second menus would then be added as submenus of the third. In XML format, this would look something like the following: .. code-block:: :dedent:
The following example is exactly equivalent. It is more illustrative of the exact relationship between the menus and items (keeping in mind that the 'link' element defines a new menu that is linked to the containing one). The style of the second example is more verbose and difficult to read (and therefore not recommended except for the purpose of understanding what is really going on). .. code-block:: :dedent: .. versionadded:: 2.32 :param label: the section label, or :const:`None` :param section: a :obj:`~gi.repository.Gio.MenuModel` with the items of the section .. classmethod:: new_submenu(label: str | None, submenu: ~gi.repository.Gio.MenuModel) -> ~gi.repository.Gio.MenuItem Creates a new :obj:`~gi.repository.Gio.MenuItem` representing a submenu. This is a convenience API around :func:`~gi.repository.Gio.MenuItem.new` and :func:`~gi.repository.Gio.MenuItem.set_submenu`. .. versionadded:: 2.32 :param label: the section label, or :const:`None` :param submenu: a :obj:`~gi.repository.Gio.MenuModel` with the items of the submenu Methods ------- .. rst-class:: interim-class .. class:: MenuItem :no-index: .. method:: get_attribute_value(attribute: str, expected_type: ~gi.repository.GLib.VariantType | None = None) -> ~gi.repository.GLib.Variant | None Queries the named ``attribute`` on ``menu_item``. If ``expected_type`` is specified and the attribute does not have this type, :const:`None` is returned. :const:`None` is also returned if the attribute simply does not exist. .. versionadded:: 2.34 :param attribute: the attribute name to query :param expected_type: the expected type of the attribute .. method:: get_link(link: str) -> ~gi.repository.Gio.MenuModel | None Queries the named ``link`` on ``menu_item``. .. versionadded:: 2.34 :param link: the link name to query .. method:: set_action_and_target_value(action: str | None = None, target_value: ~gi.repository.GLib.Variant | None = None) -> None Sets or unsets the "action" and "target" attributes of ``menu_item``. If ``action`` is :const:`None` then both the "action" and "target" attributes are unset (and ``target_value`` is ignored). If ``action`` is non-:const:`None` then the "action" attribute is set. The "target" attribute is then set to the value of ``target_value`` if it is non-:const:`None` or unset otherwise. Normal menu items (ie: not submenu, section or other custom item types) are expected to have the "action" attribute set to identify the action that they are associated with. The state type of the action help to determine the disposition of the menu item. See :obj:`~gi.repository.Gio.Action` and :obj:`~gi.repository.Gio.ActionGroup` for an overview of actions. In general, clicking on the menu item will result in activation of the named action with the "target" attribute given as the parameter to the action invocation. If the "target" attribute is not set then the action is invoked with no parameter. If the action has no state then the menu item is usually drawn as a plain menu item (ie: with no additional decoration). If the action has a boolean state then the menu item is usually drawn as a toggle menu item (ie: with a checkmark or equivalent indication). The item should be marked as 'toggled' or 'checked' when the boolean state is :const:`True`. If the action has a string state then the menu item is usually drawn as a radio menu item (ie: with a radio bullet or equivalent indication). The item should be marked as 'selected' when the string state is equal to the value of the ``target`` property. See :func:`~gi.repository.Gio.MenuItem.set_action_and_target` or :func:`~gi.repository.Gio.MenuItem.set_detailed_action` for two equivalent calls that are probably more convenient for most uses. .. versionadded:: 2.32 :param action: the name of the action for this item :param target_value: a :obj:`~gi.repository.GLib.Variant` to use as the action target .. method:: set_attribute(attributes) Sets or unsets an attribute on ``menu_item``. The attribute to set or unset is specified by ``attribute``. This can be one of the standard attribute names :const:`~gi.repository.Gio.MENU_ATTRIBUTE_LABEL`, :const:`~gi.repository.Gio.MENU_ATTRIBUTE_ACTION`, :const:`~gi.repository.Gio.MENU_ATTRIBUTE_TARGET`, or a custom attribute name. Attribute names are restricted to lowercase characters, numbers and '-'. Furthermore, the names must begin with a lowercase character, must not end with a '-', and must not contain consecutive dashes. If ``format_string`` is non-:const:`None` then the proper position parameters are collected to create a :obj:`~gi.repository.GLib.Variant` instance to use as the attribute value. If it is :const:`None` then the positional parameterrs are ignored and the named attribute is unset. See also :func:`~gi.repository.Gio.MenuItem.set_attribute_value` for an equivalent call that directly accepts a :obj:`~gi.repository.GLib.Variant`. .. versionadded:: 2.32 :param attributes: .. method:: set_attribute_value(attribute: str, value: ~gi.repository.GLib.Variant | None = None) -> None Sets or unsets an attribute on ``menu_item``. The attribute to set or unset is specified by ``attribute``. This can be one of the standard attribute names :const:`~gi.repository.Gio.MENU_ATTRIBUTE_LABEL`, :const:`~gi.repository.Gio.MENU_ATTRIBUTE_ACTION`, :const:`~gi.repository.Gio.MENU_ATTRIBUTE_TARGET`, or a custom attribute name. Attribute names are restricted to lowercase characters, numbers and '-'. Furthermore, the names must begin with a lowercase character, must not end with a '-', and must not contain consecutive dashes. must consist only of lowercase ASCII characters, digits and '-'. If ``value`` is non-:const:`None` then it is used as the new value for the attribute. If ``value`` is :const:`None` then the attribute is unset. If the ``value`` :obj:`~gi.repository.GLib.Variant` is floating, it is consumed. See also :func:`~gi.repository.Gio.MenuItem.set_attribute` for a more convenient way to do the same. .. versionadded:: 2.32 :param attribute: the attribute to set :param value: a :obj:`~gi.repository.GLib.Variant` to use as the value, or :const:`None` .. method:: set_detailed_action(detailed_action: str) -> None Sets the "action" and possibly the "target" attribute of ``menu_item``. The format of ``detailed_action`` is the same format parsed by :func:`~gi.repository.Gio.Action.parse_detailed_name`. See :func:`~gi.repository.Gio.MenuItem.set_action_and_target` or :func:`~gi.repository.Gio.MenuItem.set_action_and_target_value` for more flexible (but slightly less convenient) alternatives. See also :func:`~gi.repository.Gio.MenuItem.set_action_and_target_value` for a description of the semantics of the action and target attributes. .. versionadded:: 2.32 :param detailed_action: the "detailed" action string .. method:: set_icon(icon: ~gi.repository.Gio.Icon) -> None Sets (or unsets) the icon on ``menu_item``. This call is the same as calling :func:`~gi.repository.Gio.Icon.serialize` and using the result as the value to :func:`~gi.repository.Gio.MenuItem.set_attribute_value` for :const:`~gi.repository.Gio.MENU_ATTRIBUTE_ICON`. This API is only intended for use with "noun" menu items; things like bookmarks or applications in an "Open With" menu. Don't use it on menu items corresponding to verbs (eg: stock icons for 'Save' or 'Quit'). If ``icon`` is :const:`None` then the icon is unset. .. versionadded:: 2.38 :param icon: a :obj:`~gi.repository.Gio.Icon`, or :const:`None` .. method:: set_label(label: str | None = None) -> None Sets or unsets the "label" attribute of ``menu_item``. If ``label`` is non-:const:`None` it is used as the label for the menu item. If it is :const:`None` then the label attribute is unset. .. versionadded:: 2.32 :param label: the label to set, or :const:`None` to unset .. method:: set_link(link: str, model: ~gi.repository.Gio.MenuModel | None = None) -> None Creates a link from ``menu_item`` to ``model`` if non-:const:`None`, or unsets it. Links are used to establish a relationship between a particular menu item and another menu. For example, :const:`~gi.repository.Gio.MENU_LINK_SUBMENU` is used to associate a submenu with a particular menu item, and :const:`~gi.repository.Gio.MENU_LINK_SECTION` is used to create a section. Other types of link can be used, but there is no guarantee that clients will be able to make sense of them. Link types are restricted to lowercase characters, numbers and '-'. Furthermore, the names must begin with a lowercase character, must not end with a '-', and must not contain consecutive dashes. .. versionadded:: 2.32 :param link: type of link to establish or unset :param model: the :obj:`~gi.repository.Gio.MenuModel` to link to (or :const:`None` to unset) .. method:: set_section(section: ~gi.repository.Gio.MenuModel | None = None) -> None Sets or unsets the "section" link of ``menu_item`` to ``section``. The effect of having one menu appear as a section of another is exactly as it sounds: the items from ``section`` become a direct part of the menu that ``menu_item`` is added to. See :func:`~gi.repository.Gio.MenuItem.new_section` for more information about what it means for a menu item to be a section. .. versionadded:: 2.32 :param section: a :obj:`~gi.repository.Gio.MenuModel`, or :const:`None` .. method:: set_submenu(submenu: ~gi.repository.Gio.MenuModel | None = None) -> None Sets or unsets the "submenu" link of ``menu_item`` to ``submenu``. If ``submenu`` is non-:const:`None`, it is linked to. If it is :const:`None` then the link is unset. The effect of having one menu appear as a submenu of another is exactly as it sounds. .. versionadded:: 2.32 :param submenu: a :obj:`~gi.repository.Gio.MenuModel`, or :const:`None`