Search code examples
menupygtkgtkkey-bindingsaccelerator

How to change accel keybindings in gtk+ menu programmatically?


Coded in pygtk, I created the program menu this way:

def _create_menu
    manager = self._window.get_ui_manager()
    self.action_group = gtk.ActionGroup("SomeActions")
    self.action_group.add_actions([
        ("Top", None, _("Main Menu")),
        ("Test", None, _("Test"), self.shortcut[0], _("Test1"), self.cb_on_test),
        ("Other", None, _("Other"), self.shortcut[1], _("Test2"), self.cb_on_other)])
    manager.insert_action_group(self.action_group, -1)
    self.ui_id = manager.add_ui_from_string(ui_str)

The menu shows up as expected, but what I don't understand is: how to change accelerator keys previously assigned to the menu from other methods and/or modules?
Changing accel's variable values (shortcut[0] and shortcut[1]) did not automatically updates the menu entry. Please help


Solution

  • You can do it like this. (Disclaimer: I'm translating it from C on the fly)

    gtk.accel_map_change_entry('<Actions>/SomeActions/Test', gtk.keysyms.t, gtk.gdk.CONTROL_MASK, False)
    

    This makes it Ctrl-T, for example. The False means don't set the new accelerator if Ctrl-T is already used.