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
  • MenuItem

MenuItem#

Added in version 2.32.

class MenuItem(**properties: Any)#

Superclasses: Object

MenuItem is an opaque structure type. You must access it using the functions below.

Constructors#

class MenuItem
classmethod new(label: str | None = None, detailed_action: str | None = None) → MenuItem#

Creates a new MenuItem.

If label is non-None it is used to set the “label” attribute of the new item.

If detailed_action is non-None it is used to set the “action” and possibly the “target” attribute of the new item. See set_detailed_action() for more information.

Added in version 2.32.

Parameters:
  • label – the section label, or None

  • detailed_action – the detailed action string, or None

classmethod new_from_model(model: MenuModel, item_index: int) → MenuItem#

Creates a MenuItem as an exact copy of an existing menu item in a MenuModel.

item_index must be valid (ie: be sure to call get_n_items() first).

Added in version 2.34.

Parameters:
  • model – a MenuModel

  • item_index – the index of an item in model

classmethod new_section(label: str | None, section: MenuModel) → MenuItem#

Creates a new MenuItem representing a section.

This is a convenience API around new() and 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-None then it will be incorporated 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 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:

<menu id='edit-menu'>
  <section>
    <item label='Undo'/>
    <item label='Redo'/>
  </section>
  <section>
    <item label='Cut'/>
    <item label='Copy'/>
    <item label='Paste'/>
  </section>
</menu>

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).

<menu id='edit-menu'>
  <item>
    <link name='section'>
      <item label='Undo'/>
      <item label='Redo'/>
    </link>
  </item>
  <item>
    <link name='section'>
      <item label='Cut'/>
      <item label='Copy'/>
      <item label='Paste'/>
    </link>
  </item>
</menu>

Added in version 2.32.

Parameters:
  • label – the section label, or None

  • section – a MenuModel with the items of the section

classmethod new_submenu(label: str | None, submenu: MenuModel) → MenuItem#

Creates a new MenuItem representing a submenu.

This is a convenience API around new() and set_submenu().

Added in version 2.32.

Parameters:
  • label – the section label, or None

  • submenu – a MenuModel with the items of the submenu

Methods#

class MenuItem
get_attribute_value(attribute: str, expected_type: VariantType | None = None) → Variant | None#

Queries the named attribute on menu_item.

If expected_type is specified and the attribute does not have this type, None is returned. None is also returned if the attribute simply does not exist.

Added in version 2.34.

Parameters:
  • attribute – the attribute name to query

  • expected_type – the expected type of the attribute

get_link(link: str) → MenuModel | None#

Queries the named link on menu_item.

Added in version 2.34.

Parameters:

link – the link name to query

set_action_and_target_value(action: str | None = None, target_value: Variant | None = None) → None#

Sets or unsets the “action” and “target” attributes of menu_item.

If action is None then both the “action” and “target” attributes are unset (and target_value is ignored).

If action is non-None then the “action” attribute is set. The “target” attribute is then set to the value of target_value if it is non-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 Action and 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 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 set_action_and_target() or set_detailed_action() for two equivalent calls that are probably more convenient for most uses.

Added in version 2.32.

Parameters:
  • action – the name of the action for this item

  • target_value – a Variant to use as the action target

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 MENU_ATTRIBUTE_LABEL, MENU_ATTRIBUTE_ACTION, 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-None then the proper position parameters are collected to create a Variant instance to use as the attribute value. If it is None then the positional parameterrs are ignored and the named attribute is unset.

See also set_attribute_value() for an equivalent call that directly accepts a Variant.

Added in version 2.32.

Parameters:

attributes

set_attribute_value(attribute: str, value: 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 MENU_ATTRIBUTE_LABEL, MENU_ATTRIBUTE_ACTION, 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-None then it is used as the new value for the attribute. If value is None then the attribute is unset. If the value Variant is floating, it is consumed.

See also set_attribute() for a more convenient way to do the same.

Added in version 2.32.

Parameters:
  • attribute – the attribute to set

  • value – a Variant to use as the value, or None

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 parse_detailed_name().

See set_action_and_target() or set_action_and_target_value() for more flexible (but slightly less convenient) alternatives.

See also set_action_and_target_value() for a description of the semantics of the action and target attributes.

Added in version 2.32.

Parameters:

detailed_action – the “detailed” action string

set_icon(icon: Icon) → None#

Sets (or unsets) the icon on menu_item.

This call is the same as calling serialize() and using the result as the value to set_attribute_value() for 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 None then the icon is unset.

Added in version 2.38.

Parameters:

icon – a Icon, or None

set_label(label: str | None = None) → None#

Sets or unsets the “label” attribute of menu_item.

If label is non-None it is used as the label for the menu item. If it is None then the label attribute is unset.

Added in version 2.32.

Parameters:

label – the label to set, or None to unset

set_link(link: str, model: MenuModel | None = None) → None#

Creates a link from menu_item to model if non-None, or unsets it.

Links are used to establish a relationship between a particular menu item and another menu. For example, MENU_LINK_SUBMENU is used to associate a submenu with a particular menu item, and 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.

Added in version 2.32.

Parameters:
  • link – type of link to establish or unset

  • model – the MenuModel to link to (or None to unset)

set_section(section: 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 new_section() for more information about what it means for a menu item to be a section.

Added in version 2.32.

Parameters:

section – a MenuModel, or None

set_submenu(submenu: MenuModel | None = None) → None#

Sets or unsets the “submenu” link of menu_item to submenu.

If submenu is non-None, it is linked to. If it is None then the link is unset.

The effect of having one menu appear as a submenu of another is exactly as it sounds.

Added in version 2.32.

Parameters:

submenu – a MenuModel, or None

On this page
  • Constructors
    • MenuItem.new()
    • MenuItem.new_from_model()
    • MenuItem.new_section()
    • MenuItem.new_submenu()
  • Methods
    • MenuItem.get_attribute_value()
    • MenuItem.get_link()
    • MenuItem.set_action_and_target_value()
    • MenuItem.set_attribute()
    • MenuItem.set_attribute_value()
    • MenuItem.set_detailed_action()
    • MenuItem.set_icon()
    • MenuItem.set_label()
    • MenuItem.set_link()
    • MenuItem.set_section()
    • MenuItem.set_submenu()

This Page

  • Show Source

Created using Sphinx 8.2.3.

General Index

Built with the PyData Sphinx Theme 0.16.1.