How to display an accelerator for Gio.MenuItem

I'm trying to show a keyboard shortcut accelerator for Gio.Menuitem

pic

As you can see, the RandomAlbum menu item does not display the accelerator, however I added the added accelerator and connected it to Gio.MenuItem successfully, because the menu element responds to the Alt + Z key combination

The code snippet I'm using looks like this:

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 is not showing, but still responding to keyboard control?

The full source is here:

+4
source share
1 answer

Part of this puzzle is missing, realizing that Gio.MenuItems themselves have attribute values.

So, in this case, before adding a menu item to GMenu, the syntax is required:

 item.set_attribute_value("accel", GLib.Variant("s", "<Alt>Z")) 

To fulfill the answer, you can also set the shortcut and action for the menu item as follows:

  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 set_label and set_detailed_action do the same.

+7
source

Source: https://habr.com/ru/post/1483616/


All Articles