Menu#
Added in version 2.32.
Superclasses: MenuModel, Object
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 insert. To add a section, use
insert_section. To add a submenu, use
insert_submenu.
Constructors#
Methods#
- class Menu
- append(label: str | None = None, detailed_action: str | None = None) → None#
Convenience function for appending a normal menu item to the end of
menu. Combinenew()andinsert_item()for a more flexible alternative.Added in version 2.32.
- Parameters:
label – the section label, or
Nonedetailed_action – the detailed action string, or
None
- append_item(item: MenuItem) → None#
Appends
itemto the end ofmenu.See
insert_item()for more information.Added in version 2.32.
- Parameters:
item – a
MenuItemto append
- append_section(label: str | None, section: MenuModel) → None#
Convenience function for appending a section menu item to the end of
menu. Combinenew_section()andinsert_item()for a more flexible alternative.Added in version 2.32.
- Parameters:
label – the section label, or
Nonesection – a
MenuModelwith the items of the section
- append_submenu(label: str | None, submenu: MenuModel) → None#
Convenience function for appending a submenu menu item to the end of
menu. Combinenew_submenu()andinsert_item()for a more flexible alternative.Added in version 2.32.
- Parameters:
label – the section label, or
Nonesubmenu – a
MenuModelwith the items of the submenu
- freeze() → None#
Marks
menuas frozen.After the menu is frozen, it is an error to attempt to make any changes to it. In effect this means that the
MenuAPI must no longer be used.This function causes
is_mutable()to begin returningFalse, which has some positive performance implications.Added in version 2.32.
- insert(position: int, label: str | None = None, detailed_action: str | None = None) → None#
Convenience function for inserting a normal menu item into
menu. Combinenew()andinsert_item()for a more flexible alternative.Added in version 2.32.
- Parameters:
position – the position at which to insert the item
label – the section label, or
Nonedetailed_action – the detailed action string, or
None
- insert_item(position: int, item: MenuItem) → None#
Inserts
itemintomenu.The “insertion” is actually done by copying all of the attribute and link values of
itemand using them to form a new item withinmenu. As such,itemitself is not really inserted, but rather, a menu item that is exactly the same as the one presently described byitem.This means that
itemis 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
itemonce you’re done.There are many convenience functions to take care of common cases. See
insert(),insert_section()andinsert_submenu()as well as “prepend” and “append” variants of each of these functions.Added in version 2.32.
- Parameters:
position – the position at which to insert the item
item – the
MenuItemto insert
- insert_section(position: int, label: str | None, section: MenuModel) → None#
Convenience function for inserting a section menu item into
menu. Combinenew_section()andinsert_item()for a more flexible alternative.Added in version 2.32.
- Parameters:
position – the position at which to insert the item
label – the section label, or
Nonesection – a
MenuModelwith the items of the section
- insert_submenu(position: int, label: str | None, submenu: MenuModel) → None#
Convenience function for inserting a submenu menu item into
menu. Combinenew_submenu()andinsert_item()for a more flexible alternative.Added in version 2.32.
- Parameters:
position – the position at which to insert the item
label – the section label, or
Nonesubmenu – a
MenuModelwith the items of the submenu
- prepend(label: str | None = None, detailed_action: str | None = None) → None#
Convenience function for prepending a normal menu item to the start of
menu. Combinenew()andinsert_item()for a more flexible alternative.Added in version 2.32.
- Parameters:
label – the section label, or
Nonedetailed_action – the detailed action string, or
None
- prepend_item(item: MenuItem) → None#
Prepends
itemto the start ofmenu.See
insert_item()for more information.Added in version 2.32.
- Parameters:
item – a
MenuItemto prepend
- prepend_section(label: str | None, section: MenuModel) → None#
Convenience function for prepending a section menu item to the start of
menu. Combinenew_section()andinsert_item()for a more flexible alternative.Added in version 2.32.
- Parameters:
label – the section label, or
Nonesection – a
MenuModelwith the items of the section
- prepend_submenu(label: str | None, submenu: MenuModel) → None#
Convenience function for prepending a submenu menu item to the start of
menu. Combinenew_submenu()andinsert_item()for a more flexible alternative.Added in version 2.32.
- Parameters:
label – the section label, or
Nonesubmenu – a
MenuModelwith the items of the submenu
- remove(position: int) → None#
Removes an item from the menu.
positiongives 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).
Added in version 2.32.
- Parameters:
position – the position of the item to remove