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
None
detailed_action – the detailed action string, or
None
- append_item(item: MenuItem) → None
Appends
item
to the end ofmenu
.See
insert_item()
for more information.Added in version 2.32.
- Parameters:
item – a
MenuItem
to 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
None
section – a
MenuModel
with 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
None
submenu – a
MenuModel
with the items of the submenu
- freeze() → 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
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
None
detailed_action – the detailed action string, or
None
- insert_item(position: int, item: MenuItem) → None
Inserts
item
intomenu
.The “insertion” is actually done by copying all of the attribute and link values of
item
and using them to form a new item withinmenu
. As such,item
itself is not really inserted, but rather, a menu item that is exactly the same as the one presently described byitem
.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
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
MenuItem
to 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
None
section – a
MenuModel
with 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
None
submenu – a
MenuModel
with 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
None
detailed_action – the detailed action string, or
None
- prepend_item(item: MenuItem) → None
Prepends
item
to the start ofmenu
.See
insert_item()
for more information.Added in version 2.32.
- Parameters:
item – a
MenuItem
to 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
None
section – a
MenuModel
with 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
None
submenu – a
MenuModel
with the items of the submenu
- remove(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).
Added in version 2.32.
- Parameters:
position – the position of the item to remove