A GtkApplication with a simple GMenu and SimpleActions
In line 33 the signal "activate"
from the action new_action
(not the menu!) is connected to the callback function new_cb()
using action.connect(signal, callback function)
. See for a more detailed explanation.
Useful methods for a GSimpleAction:
To create a new action that is stateless, that is, an action that do not retain or depend on a state given by the action itself, use
action = Gio.SimpleAction.new("name", parameter_type)
where "name"
is the name of the action and parameter_type
is the type of the parameters that the action receives when being activated. This can be None
, or GLib.VariantType.new('s')
if the parameter is of type str
, or instead of 's'
a character as described here. To create a new stateful (i.e. not stateless) action, use
action = Gio.SimpleAction.new_stateful("name", parameter_type, initial_state)
where initial_state
is defined as a GVariant - for instance Glib.Variant.new_string('start')
; for a list of possibilities see here.
set_enabled(True)
sets the action as enabled; an action must be enabled in order to be activated or in order to have its state changed from outside callers. This should only be called by the implementor of the action. Users of the action should not attempt to modify its enabled flag.
set_state(state)
, where state
is a GVariant, sets the state of the action, updating the 'state' property to the given value. This should only be called by the implementor of the action; users of the action should instead call change_state(state)
(where state
is as above) to request the change.
Useful methods for a GMenu:
To insert an item in the menu in position position
, use insert(position, label, detailed_action)
, where label
is the label that will appear in the menu and detailed_action
is a string composed of the name of the action to which we prepend the prefix app.
. A more detailed discussion of this can be found in .
To append or prepend an item in the menu use respectively append(label, detailed_action)
and prepend(label, detailed_action)
.
Another way of adding items to the menu is to create them as GMenuItem
s and use insert_item(position, item)
, append_item(item)
, or prepend_item(item)
; so for instance we might have:
about = Gio.MenuItem.new("About", "app.about")
menu.append_item(about)
We can also add a whole subsection in a menu using insert_section(position, label, section)
, append_section(label, section)
, or prepend_section(label, section)
, where label
is the title of the subsection.
To add a submenu that will expand and collapse, use insert_submenu(position, label, section)
, append_submenu(label, section)
, or prepend_submenu(label, section)
, where label
is the title of the subsection.
To remove an item from the menu, use remove(position)
.
To set a label for the menu, use set_label(label)
.
In this sample we used the following:
GMenu
GSimpleAction
GVariantType
GVariant