Skip to content

Gio.Menu

class — extends MenuModel

GMenu is a simple implementation of MenuModel. You populate a GMenu by adding MenuItem instances to it.

There are some convenience functions to allow you to directly add items (avoiding MenuItem) for the common cases. To add a regular item, use Menu.insert. To add a section, use Menu.insert_section. To add a submenu, use Menu.insert_submenu.

Constructors

new

@classmethod
def new(cls) -> Menu

Creates a new Menu.

The new menu has no items.

Methods

append

def append(self, label: str | None = ..., detailed_action: str | None = ...) -> None

Convenience function for appending a normal menu item to the end of menu. Combine MenuItem.new and Menu.insert_item for a more flexible alternative.

Parameters:

  • label — the section label, or None
  • detailed_action — the detailed action string, or None

append_item

def append_item(self, item: MenuItem) -> None

Appends item to the end of menu.

See Menu.insert_item for more information.

Parameters:

append_section

def append_section(self, label: str | None, section: MenuModel) -> None

Convenience function for appending a section menu item to the end of menu. Combine MenuItem.new_section and Menu.insert_item for a more flexible alternative.

Parameters:

  • label — the section label, or None
  • section — a MenuModel with the items of the section

append_submenu

def append_submenu(self, label: str | None, submenu: MenuModel) -> None

Convenience function for appending a submenu menu item to the end of menu. Combine MenuItem.new_submenu and Menu.insert_item for a more flexible alternative.

Parameters:

  • label — the section label, or None
  • submenu — a MenuModel with the items of the submenu

freeze

def freeze(self) -> None

Marks menu as frozen.

After the menu is frozen, it is an error to attempt to make any changes to it. In effect this means that the Menu API must no longer be used.

This function causes MenuModel.is_mutable to begin returning False, which has some positive performance implications.

insert

def insert(self, position: int, label: str | None = ..., detailed_action: str | None = ...) -> None

Convenience function for inserting a normal menu item into menu. Combine MenuItem.new and Menu.insert_item for a more flexible alternative.

Parameters:

  • position — the position at which to insert the item
  • label — the section label, or None
  • detailed_action — the detailed action string, or None

insert_item

def insert_item(self, position: int, item: MenuItem) -> None

Inserts item into menu.

The "insertion" is actually done by copying all of the attribute and link values of item and using them to form a new item within menu. As such, item itself is not really inserted, but rather, a menu item that is exactly the same as the one presently described by item.

This means that item is essentially useless after the insertion occurs. Any changes you make to it are ignored unless it is inserted again (at which point its updated values will be copied).

You should probably just free item once you're done.

There are many convenience functions to take care of common cases. See Menu.insert, Menu.insert_section and Menu.insert_submenu as well as "prepend" and "append" variants of each of these functions.

Parameters:

  • position — the position at which to insert the item
  • item — the MenuItem to insert

insert_section

def insert_section(self, position: int, label: str | None, section: MenuModel) -> None

Convenience function for inserting a section menu item into menu. Combine MenuItem.new_section and Menu.insert_item for a more flexible alternative.

Parameters:

  • position — the position at which to insert the item
  • label — the section label, or None
  • section — a MenuModel with the items of the section

insert_submenu

def insert_submenu(self, position: int, label: str | None, submenu: MenuModel) -> None

Convenience function for inserting a submenu menu item into menu. Combine MenuItem.new_submenu and Menu.insert_item for a more flexible alternative.

Parameters:

  • position — the position at which to insert the item
  • label — the section label, or None
  • submenu — a MenuModel with the items of the submenu

prepend

def prepend(self, label: str | None = ..., detailed_action: str | None = ...) -> None

Convenience function for prepending a normal menu item to the start of menu. Combine MenuItem.new and Menu.insert_item for a more flexible alternative.

Parameters:

  • label — the section label, or None
  • detailed_action — the detailed action string, or None

prepend_item

def prepend_item(self, item: MenuItem) -> None

Prepends item to the start of menu.

See Menu.insert_item for more information.

Parameters:

prepend_section

def prepend_section(self, label: str | None, section: MenuModel) -> None

Convenience function for prepending a section menu item to the start of menu. Combine MenuItem.new_section and Menu.insert_item for a more flexible alternative.

Parameters:

  • label — the section label, or None
  • section — a MenuModel with the items of the section

prepend_submenu

def prepend_submenu(self, label: str | None, submenu: MenuModel) -> None

Convenience function for prepending a submenu menu item to the start of menu. Combine MenuItem.new_submenu and Menu.insert_item for a more flexible alternative.

Parameters:

  • label — the section label, or None
  • submenu — a MenuModel with the items of the submenu

remove

def remove(self, position: int) -> None

Removes an item from the menu.

position gives the index of the item to remove.

It is an error if position is not in range the range from 0 to one less than the number of items in the menu.

It is not possible to remove items by identity since items are added to the menu simply by copying their links and attributes (ie: identity of the item itself is not preserved).

Parameters:

  • position — the position of the item to remove

remove_all

def remove_all(self) -> None

Removes all items in the menu.