如何为Gedit插件创建的菜单项添加快捷键/加速键

3 投票
1 回答
2282 浏览
提问于 2025-04-17 05:22

我创建了一个Gedit 2的插件,这个插件可以在菜单中添加一个选项,具体的做法可以在这里找到。那么,我该如何给这个菜单选项绑定一个快捷键呢?

1 个回答

8
self._action_group.add_actions([("ExamplePy", None, _("Clear document"),
        None, _("Clear the document"),
        self.on_clear_document_activate)])

根据给出的教程,你的插件里应该有一些类似下面的代码:

self._action_group = gtk.ActionGroup("ExamplePyPluginActions")
self._action_group.add_actions([("ExamplePy", None, _("Clear document"),
         None, _("Clear the document"),
         self.on_clear_document_activate)])
manager.insert_action_group(self._action_group, -1)

只需要把第二个 None 参数替换成你想要的快捷键,比如说 ControlR

self._action_group.add_actions([("ExamplePy", None, _("Clear document"),
        "<control>r", _("Clear the document"), # <- here
        self.on_clear_document_activate)])

你可能也用过手动创建的动作(这也是我最喜欢的方式):

action = gtk.Action("ExamplePy", 
        _("Clear document"), 
        _("Clear the document"), None)
action.connect("activate", self.on_open_regex_dialog)
action_group = gtk.ActionGroup("ExamplePyPluginActions")
action_group.add_action(action)

在这种情况下,只需把 action_group.add_action() 替换成 action_group.add_action_with_accel()

action_group = gtk.ActionGroup("ExamplePyPluginActions")
action_group.add_action_with_accel(action, "<control>r")

(这个问题是我自己问的并且自己回答,因为 这个这个;我花了一些时间寻找这个信息,觉得这会是个不错的参考。)

撰写回答