I'm trying to display the shortcut key accelerator for a Gio.Menuitem
As you can see, the RandomAlbum menu item does not have an accelerator displayed - however, I have added added the accelerator and connected it to the Gio.MenuItem successfully because the menuitem responds to the keyboard shortcut Alt+Z
The snippet of code I'm using is as follows:
action = Gio.SimpleAction.new(action_name, None)
app = Gio.Application.get_default()
app.add_accelerator("<alt>Z", "app."+action_name, None)
item = Gio.MenuItem()
item.set_detailed_action('app.' + action_name)
item.set_label("RandomAlbum")
app.add_plugin_menu_item('tools', "unique name", item)
Any ideas why the accelerator does not display - but still responds to keyboard control?
The full source is here:
The missing piece of this jigsaw puzzle is realising that Gio.MenuItems themselves have attribute-values.
So in this case, before adding the menu-item to the GMenu the syntax required is:
item.set_attribute_value("accel", GLib.Variant("s", "<Alt>Z"))
To complete the answer, you can also set the label and action for the menu-item in this way:
item = Gio.MenuItem() item.set_attribute_value("label", GLib.Variant("s", "RandomAlbum")) item.set_attribute_value("action", GLib.Variant("s", "app."+action_name))
However the methods set_label
and set_detailed_action
perform the same role.