Skip to main content
Ctrl+K

GNOME Python API

  • GLib
  • Gio
  • GObject
  • Gtk
  • Gdk
  • Adw
    • DBus
    • DBusGLib
    • Flatpak
    • GModule
    • Gcr
    • GdkPixbuf
    • GdkPixdata
    • GdkWayland
    • GdkX11
    • Graphene
    • Gsk
    • Gst
    • GstBase
    • GstCheck
    • GstController
    • GstNet
    • GtkSource
    • HarfBuzz
    • ICal
    • JavaScriptCore
    • Pango
    • PangoCairo
    • PangoFT2
    • PangoFc
    • PangoOT
    • PangoXft
    • Shumate
    • Soup
    • Vte
    • WebKit
    • WebKitWebProcessExtension
    • Xdp
    • XdpGtk4
    • pycairo
  • GLib
  • Gio
  • GObject
  • Gtk
  • Gdk
  • Adw
  • DBus
  • DBusGLib
  • Flatpak
  • GModule
  • Gcr
  • GdkPixbuf
  • GdkPixdata
  • GdkWayland
  • GdkX11
  • Graphene
  • Gsk
  • Gst
  • GstBase
  • GstCheck
  • GstController
  • GstNet
  • GtkSource
  • HarfBuzz
  • ICal
  • JavaScriptCore
  • Pango
  • PangoCairo
  • PangoFT2
  • PangoFc
  • PangoOT
  • PangoXft
  • Shumate
  • Soup
  • Vte
  • WebKit
  • WebKitWebProcessExtension
  • Xdp
  • XdpGtk4
  • pycairo

Section Navigation

API

  • Classes
    • AppInfoMonitor
    • AppLaunchContext
    • Application
    • ApplicationCommandLine
    • BufferedInputStream
    • BufferedOutputStream
    • BytesIcon
    • Cancellable
    • CharsetConverter
    • ConverterInputStream
    • ConverterOutputStream
    • Credentials
    • DBusActionGroup
    • DBusAuthObserver
    • DBusConnection
    • DBusInterfaceSkeleton
    • DBusMenuModel
    • DBusMessage
    • DBusMethodInvocation
    • DBusObjectManagerClient
    • DBusObjectManagerServer
    • DBusObjectProxy
    • DBusObjectSkeleton
    • DBusProxy
    • DBusServer
    • DataInputStream
    • DataOutputStream
    • DebugControllerDBus
    • DesktopAppInfo
    • Emblem
    • EmblemedIcon
    • FileEnumerator
    • FileIOStream
    • FileIcon
    • FileInfo
    • FileInputStream
    • FileMonitor
    • FileOutputStream
    • FilenameCompleter
    • FilterInputStream
    • FilterOutputStream
    • IOModule
    • IOStream
    • InetAddress
    • InetAddressMask
    • InetSocketAddress
    • InputStream
    • ListStore
    • MemoryInputStream
    • MemoryOutputStream
    • Menu
    • MenuAttributeIter
    • MenuItem
    • MenuLinkIter
    • MenuModel
    • MountOperation
    • NativeSocketAddress
    • NativeVolumeMonitor
    • NetworkAddress
    • NetworkService
    • Notification
    • OutputStream
    • Permission
    • PropertyAction
    • ProxyAddress
    • ProxyAddressEnumerator
    • Resolver
    • Settings
    • SettingsBackend
    • SimpleAction
    • SimpleActionGroup
    • SimpleAsyncResult
    • SimpleIOStream
    • SimplePermission
    • SimpleProxyResolver
    • Socket
    • SocketAddress
    • SocketAddressEnumerator
    • SocketClient
    • SocketConnection
    • SocketControlMessage
    • SocketListener
    • SocketService
    • Subprocess
    • SubprocessLauncher
    • Task
    • TcpConnection
    • TcpWrapperConnection
    • TestDBus
    • ThemedIcon
    • ThreadedResolver
    • ThreadedSocketService
    • TlsCertificate
    • TlsConnection
    • TlsDatabase
    • TlsInteraction
    • TlsPassword
    • UnixConnection
    • UnixCredentialsMessage
    • UnixFDList
    • UnixFDMessage
    • UnixInputStream
    • UnixMountMonitor
    • UnixOutputStream
    • UnixSocketAddress
    • Vfs
    • VolumeMonitor
    • ZlibCompressor
    • ZlibDecompressor
  • Interfaces
    • Action
    • ActionGroup
    • ActionMap
    • AppInfo
    • AsyncInitable
    • AsyncResult
    • Converter
    • DBusInterface
    • DBusObject
    • DBusObjectManager
    • DatagramBased
    • DebugController
    • DesktopAppInfoLookup
    • Drive
    • DtlsClientConnection
    • DtlsConnection
    • DtlsServerConnection
    • File
    • FileDescriptorBased
    • Icon
    • Initable
    • ListModel
    • LoadableIcon
    • MemoryMonitor
    • Mount
    • NetworkMonitor
    • PollableInputStream
    • PollableOutputStream
    • PowerProfileMonitor
    • Proxy
    • ProxyResolver
    • RemoteActionGroup
    • Seekable
    • SocketConnectable
    • TlsBackend
    • TlsClientConnection
    • TlsFileDatabase
    • TlsServerConnection
    • Volume
  • Structures
    • ActionEntry
    • DBusAnnotationInfo
    • DBusArgInfo
    • DBusErrorEntry
    • DBusInterfaceInfo
    • DBusInterfaceVTable
    • DBusMethodInfo
    • DBusNodeInfo
    • DBusPropertyInfo
    • DBusSignalInfo
    • DBusSubtreeVTable
    • FileAttributeInfo
    • FileAttributeInfoList
    • FileAttributeMatcher
    • IOExtension
    • IOExtensionPoint
    • IOModuleScope
    • IOSchedulerJob
    • IOStreamAdapter
    • InputMessage
    • InputVector
    • OutputMessage
    • OutputVector
    • Resource
    • SettingsSchema
    • SettingsSchemaKey
    • SettingsSchemaSource
    • SrvTarget
    • StaticResource
    • UnixMountEntry
    • UnixMountPoint
  • Enums
    • AppInfoCreateFlags
    • ApplicationFlags
    • AskPasswordFlags
    • BusNameOwnerFlags
    • BusNameWatcherFlags
    • BusType
    • ConverterFlags
    • ConverterResult
    • CredentialsType
    • DBusCallFlags
    • DBusCapabilityFlags
    • DBusConnectionFlags
    • DBusError
    • DBusInterfaceSkeletonFlags
    • DBusMessageByteOrder
    • DBusMessageFlags
    • DBusMessageHeaderField
    • DBusMessageType
    • DBusObjectManagerClientFlags
    • DBusPropertyInfoFlags
    • DBusProxyFlags
    • DBusSendMessageFlags
    • DBusServerFlags
    • DBusSignalFlags
    • DBusSubtreeFlags
    • DataStreamByteOrder
    • DataStreamNewlineType
    • DriveStartFlags
    • DriveStartStopType
    • EmblemOrigin
    • FileAttributeInfoFlags
    • FileAttributeStatus
    • FileAttributeType
    • FileCopyFlags
    • FileCreateFlags
    • FileMeasureFlags
    • FileMonitorEvent
    • FileMonitorFlags
    • FileQueryInfoFlags
    • FileType
    • FilesystemPreviewType
    • IOErrorEnum
    • IOModuleScopeFlags
    • IOStreamSpliceFlags
    • MemoryMonitorWarningLevel
    • MountMountFlags
    • MountOperationResult
    • MountUnmountFlags
    • NetworkConnectivity
    • NotificationPriority
    • OutputStreamSpliceFlags
    • PasswordSave
    • PollableReturn
    • ResolverError
    • ResolverNameLookupFlags
    • ResolverRecordType
    • ResourceError
    • ResourceFlags
    • ResourceLookupFlags
    • SettingsBindFlags
    • SocketClientEvent
    • SocketFamily
    • SocketListenerEvent
    • SocketMsgFlags
    • SocketProtocol
    • SocketType
    • SubprocessFlags
    • TestDBusFlags
    • TlsAuthenticationMode
    • TlsCertificateFlags
    • TlsCertificateRequestFlags
    • TlsChannelBindingError
    • TlsChannelBindingType
    • TlsDatabaseLookupFlags
    • TlsDatabaseVerifyFlags
    • TlsError
    • TlsInteractionResult
    • TlsPasswordFlags
    • TlsProtocolVersion
    • TlsRehandshakeMode
    • UnixSocketAddressType
    • ZlibCompressorFormat
  • Functions
  • Constants
  • Gio
  • Classes
  • MenuModel

MenuModel#

Added in version 2.32.

class MenuModel(**properties: Any)#

Superclasses: Object

Subclasses: DBusMenuModel, Menu

GMenuModel represents the contents of a menu — an ordered list of menu items. The items are associated with actions, which can be activated through them. Items can be grouped in sections, and may have submenus associated with them. Both items and sections usually have some representation data, such as labels or icons. The type of the associated action (ie whether it is stateful, and what kind of state it has) can influence the representation of the item.

The conceptual model of menus in GMenuModel is hierarchical: sections and submenus are again represented by GMenuModels. Menus themselves do not define their own roles. Rather, the role of a particular GMenuModel is defined by the item that references it (or, in the case of the ‘root’ menu, is defined by the context in which it is used).

As an example, consider the visible portions of this menu:

An example menu#

https://docs.gtk.org/gio/menu-example.png

While this kind of deeply nested menu is no longer considered good UI practice, it serves as a good example of the concepts in GMenuModel. There are 8 ‘menus’ visible in the screenshot: one menubar, two submenus and 5 sections:

  • the toplevel menubar (containing 4 items)

  • the View submenu (containing 3 sections)

  • the first section of the View submenu (containing 2 items)

  • the second section of the View submenu (containing 1 item)

  • the final section of the View submenu (containing 1 item)

  • the Highlight Mode submenu (containing 2 sections)

  • the Sources section (containing 2 items)

  • the Markup section (containing 2 items)

The example illustrates the conceptual connection between these 8 menus. Each large block in the figure represents a menu and the smaller blocks within the large block represent items in that menu. Some items contain references to other menus.

A menu example#

https://docs.gtk.org/gio/menu-model-light.svg

Notice that the separators visible in the example appear nowhere in the menu model. This is because separators are not explicitly represented in the menu model. Instead, a separator is inserted between any two non-empty sections of a menu. Section items can have labels just like any other item. In that case, a display system may show a section header instead of a separator.

The motivation for this abstract model of application controls is that modern user interfaces tend to make these controls available outside the application. Examples include global menus, jumplists, dash boards, etc. To support such uses, it is necessary to ‘export’ information about actions and their representation in menus, which is exactly what the action group exporter and the menu model exporter do for ActionGroup and MenuModel. The client-side counterparts to make use of the exported information are DBusActionGroup and DBusMenuModel.

The API of GMenuModel is very generic, with iterators for the attributes and links of an item, see iterate_item_attributes and iterate_item_links. The ‘standard’ attributes and link types have predefined names: G_MENU_ATTRIBUTE_LABEL, G_MENU_ATTRIBUTE_ACTION, G_MENU_ATTRIBUTE_TARGET, G_MENU_LINK_SECTION and G_MENU_LINK_SUBMENU.

Items in a GMenuModel represent active controls if they refer to an action that can get activated when the user interacts with the menu item. The reference to the action is encoded by the string ID in the G_MENU_ATTRIBUTE_ACTION attribute. An action ID uniquely identifies an action in an action group. Which action group(s) provide actions depends on the context in which the menu model is used. E.g. when the model is exported as the application menu of a `GtkApplication <https://docs.gtk.org/gtk4/class.Application.html>`__, actions can be application-wide or window-specific (and thus come from two different action groups). By convention, the application-wide actions have names that start with app., while the names of window-specific actions start with win..

While a wide variety of stateful actions is possible, the following is the minimum that is expected to be supported by all users of exported menu information: - an action with no parameter type and no state - an action with no parameter type and boolean state - an action with string parameter type and string state

Stateless#

A stateless action typically corresponds to an ordinary menu item.

Selecting such a menu item will activate the action (with no parameter).

Boolean State#

An action with a boolean state will most typically be used with a ‘toggle’ or ‘switch’ menu item. The state can be set directly, but activating the action (with no parameter) results in the state being toggled.

Selecting a toggle menu item will activate the action. The menu item should be rendered as ‘checked’ when the state is true.

String Parameter and State#

Actions with string parameters and state will most typically be used to represent an enumerated choice over the items available for a group of radio menu items. Activating the action with a string parameter is equivalent to setting that parameter as the state.

Radio menu items, in addition to being associated with the action, will have a target value. Selecting that menu item will result in activation of the action with the target value as the parameter. The menu item should be rendered as ‘selected’ when the state of the action is equal to the target value of the menu item.

Methods#

class MenuModel
do_get_item_attribute_value(self, item_index: int, attribute: str, expected_type: VariantType | None = None) → Variant | None#
Parameters:
  • item_index

  • attribute

  • expected_type

do_get_item_attributes(self, item_index: int) → dict[str, Variant]#
Parameters:

item_index

do_get_item_link(self, item_index: int, link: str) → MenuModel | None#
Parameters:
  • item_index

  • link

do_get_item_links(self, item_index: int) → dict[str, MenuModel]#
Parameters:

item_index

do_get_n_items(self) → int#
do_is_mutable(self) → bool#
do_iterate_item_attributes(self, item_index: int) → MenuAttributeIter#
Parameters:

item_index

do_iterate_item_links(self, item_index: int) → MenuLinkIter#
Parameters:

item_index

get_item_attribute_value(item_index: int, attribute: str, expected_type: VariantType | None = None) → Variant | None#

Queries the item at position item_index in model for the attribute specified by attribute.

If expected_type is non-None then it specifies the expected type of the attribute. If it is None then any type will be accepted.

If the attribute exists and matches expected_type (or if the expected type is unspecified) then the value is returned.

If the attribute does not exist, or does not match the expected type then None is returned.

Added in version 2.32.

Parameters:
  • item_index – the index of the item

  • attribute – the attribute to query

  • expected_type – the expected type of the attribute, or None

get_item_link(item_index: int, link: str) → MenuModel | None#

Queries the item at position item_index in model for the link specified by link.

If the link exists, the linked MenuModel is returned. If the link does not exist, None is returned.

Added in version 2.32.

Parameters:
  • item_index – the index of the item

  • link – the link to query

get_n_items() → int#

Query the number of items in model.

Added in version 2.32.

is_mutable() → bool#

Queries if model is mutable.

An immutable MenuModel will never emit the MenuModel::items-changed signal. Consumers of the model may make optimisations accordingly.

Added in version 2.32.

items_changed(position: int, removed: int, added: int) → None#

Requests emission of the MenuModel::items-changed signal on model.

This function should never be called except by MenuModel subclasses. Any other calls to this function will very likely lead to a violation of the interface of the model.

The implementation should update its internal representation of the menu before emitting the signal. The implementation should further expect to receive queries about the new state of the menu (and particularly added menu items) while signal handlers are running.

The implementation must dispatch this call directly from a mainloop entry and not in response to calls – particularly those from the MenuModel API. Said another way: the menu must not change while user code is running without returning to the mainloop.

Added in version 2.32.

Parameters:
  • position – the position of the change

  • removed – the number of items removed

  • added – the number of items added

iterate_item_attributes(item_index: int) → MenuAttributeIter#

Creates a MenuAttributeIter to iterate over the attributes of the item at position item_index in model.

You must free the iterator with unref() when you are done.

Added in version 2.32.

Parameters:

item_index – the index of the item

iterate_item_links(item_index: int) → MenuLinkIter#

Creates a MenuLinkIter to iterate over the links of the item at position item_index in model.

You must free the iterator with unref() when you are done.

Added in version 2.32.

Parameters:

item_index – the index of the item

Signals#

class MenuModel.signals
items_changed(position: int, removed: int, added: int) → None#

The type of the None singleton.

Parameters:
  • position – the position of the change

  • removed – the number of items removed

  • added – the number of items added

Virtual Methods#

class MenuModel
do_get_item_attribute_value(item_index: int, attribute: str, expected_type: VariantType | None = None) → Variant | None#

Queries the item at position item_index in model for the attribute specified by attribute.

If expected_type is non-None then it specifies the expected type of the attribute. If it is None then any type will be accepted.

If the attribute exists and matches expected_type (or if the expected type is unspecified) then the value is returned.

If the attribute does not exist, or does not match the expected type then None is returned.

Added in version 2.32.

Parameters:
  • item_index – the index of the item

  • attribute – the attribute to query

  • expected_type – the expected type of the attribute, or None

do_get_item_attributes(item_index: int) → dict[str, Variant]#

The type of the None singleton.

Parameters:

item_index – The MenuItem to query

do_get_item_link(item_index: int, link: str) → MenuModel | None#

Queries the item at position item_index in model for the link specified by link.

If the link exists, the linked MenuModel is returned. If the link does not exist, None is returned.

Added in version 2.32.

Parameters:
  • item_index – the index of the item

  • link – the link to query

do_get_item_links(item_index: int) → dict[str, MenuModel]#

The type of the None singleton.

Parameters:

item_index – The MenuItem to query

do_get_n_items() → int#

Query the number of items in model.

Added in version 2.32.

do_is_mutable() → bool#

Queries if model is mutable.

An immutable MenuModel will never emit the MenuModel::items-changed signal. Consumers of the model may make optimisations accordingly.

Added in version 2.32.

do_iterate_item_attributes(item_index: int) → MenuAttributeIter#

Creates a MenuAttributeIter to iterate over the attributes of the item at position item_index in model.

You must free the iterator with unref() when you are done.

Added in version 2.32.

Parameters:

item_index – the index of the item

do_iterate_item_links(item_index: int) → MenuLinkIter#

Creates a MenuLinkIter to iterate over the links of the item at position item_index in model.

You must free the iterator with unref() when you are done.

Added in version 2.32.

Parameters:

item_index – the index of the item

Fields#

class MenuModel
parent_instance#
priv#
On this page
  • An example menu
  • A menu example
  • Stateless
  • Boolean State
  • String Parameter and State
  • Methods
    • MenuModel.do_get_item_attribute_value()
    • MenuModel.do_get_item_attributes()
    • MenuModel.do_get_item_link()
    • MenuModel.do_get_item_links()
    • MenuModel.do_get_n_items()
    • MenuModel.do_is_mutable()
    • MenuModel.do_iterate_item_attributes()
    • MenuModel.do_iterate_item_links()
    • MenuModel.get_item_attribute_value()
    • MenuModel.get_item_link()
    • MenuModel.get_n_items()
    • MenuModel.is_mutable()
    • MenuModel.items_changed()
    • MenuModel.iterate_item_attributes()
    • MenuModel.iterate_item_links()
  • Signals
    • MenuModel.signals.items_changed()
  • Virtual Methods
    • MenuModel.do_get_item_attribute_value()
    • MenuModel.do_get_item_attributes()
    • MenuModel.do_get_item_link()
    • MenuModel.do_get_item_links()
    • MenuModel.do_get_n_items()
    • MenuModel.do_is_mutable()
    • MenuModel.do_iterate_item_attributes()
    • MenuModel.do_iterate_item_links()
  • Fields
    • MenuModel.parent_instance
    • MenuModel.priv

This Page

  • Show Source

Created using Sphinx 7.4.7.

General Index

Built with the PyData Sphinx Theme 0.16.1.