在gedit 3插件中,Accel无法工作

3

我正在尝试为Gedit 3编写一个使用GObject内省的小插件。下面是相关代码的部分,它旨在建立一个环境,在其中可以将函数放入按钮回调中。然而,按钮的加速键无法正常工作。

  1. What is wrong with this code?
  2. I am using the tutorial here and the python documentation for GTK 3 here. Is there any other links that you know of?

    from gi.repository import Gtk, Gedit, GObject 
    ui_string = """<ui>
    <toolbar name="ToolBar">
    <separator />
    <toolitem name="Test" action="Test" />
    </toolbar>
    </ui>
    """
    class test:
        def __init__(self, plugin, window):
            self.window = window
            self.plugin = plugin
            self.ui_id = None
            manager = self.window.get_ui_manager()
            action_group = Gtk.ActionGroup("TestPluginactions")
            action_test_button = Gtk.Action(name="Test",
                label="Test",
                tooltip="Test",
                stock_id=Gtk.STOCK_EXECUTE)
    
            action_test_button.connect("activate", self.testcallback)
            action_group.add_action_with_accel(action_test_button, "<Ctrl>l")
    
            manager.insert_action_group(action_group, -1)
            self.ui_id = manager.add_ui_from_string(ui_string)
            manager.ensure_update()
    
        def deactivate(self):
            manager = self.window.get_ui_manager()
            manager.remove_ui(self.ui_id)
            self.ui_id = None
            self.window = None
            self.plugin = None
    
        def testcallback(self,unused):
            dialog1 = Gtk.MessageDialog(self.window,Gtk.DialogFlags.DESTROY_WITH_PARENT,
                Gtk.MessageType.ERROR,Gtk.ButtonsType.OK,"TEST")
            dialog1.run()
            dialog1.destroy()
    
    
    class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
        window = GObject.property(type=Gedit.Window)
        def __init__(self):
            GObject.Object.__init__(self)
            self.instances = {}
    
        def do_activate(self):
            self.instances[self.window] = test(self, self.window)
    
        def do_deactivate(self):
            if self.window in self.instances:
                self.instances[self.window].deactivate()
    
1个回答

2

了解到GTK 3存在一些错误,不允许以上述方式为工具项分配快捷键。但是菜单项可以正常工作。因此,下面的代码更好。

    from gi.repository import Gtk, Gedit, GObject 
    ui_string = """<ui>
    <toolbar name="ToolBar">
    <separator />
    <toolitem name="Test" action="Test" />
    </toolbar>
      <menubar name="MenuBar">
      <menu name="ToolsMenu" action="Tools">
      <placeholder name="ToolsOps_2">
      <menuitem name="test1" action="test1"/>
      </placeholder>
      </menu>
      </menubar>
    </ui>
    """
    class test:
        def __init__(self, plugin, window):
            self.window = window
            self.plugin = plugin
            self.ui_id = None
            manager = self.window.get_ui_manager()
            action_group = Gtk.ActionGroup("TestPluginactions")
            action_test_button = Gtk.Action(name="Test",
                label="Test",
                tooltip="Test",
                stock_id=Gtk.STOCK_EXECUTE)
            action_test = Gtk.Action(name="test1",
                label="Test",
                tooltip="Test",
                stock_id=Gtk.STOCK_EXECUTE)
            action_test_button.connect("activate", self.testcallback)
            action_test.connect("activate", self.testcallback)
            action_group.add_action(action_test_button)
            action_group.add_action_with_accel(action_test, "<Ctrl>l")

            manager.insert_action_group(action_group, -1)
            self.ui_id = manager.add_ui_from_string(ui_string)
            manager.ensure_update()

        def deactivate(self):
            manager = self.window.get_ui_manager()
            manager.remove_ui(self.ui_id)
            self.ui_id = None
            self.window = None
            self.plugin = None

        def testcallback(self,unused):
            dialog1 = Gtk.MessageDialog(self.window,Gtk.DialogFlags.DESTROY_WITH_PARENT,
                Gtk.MessageType.ERROR,Gtk.ButtonsType.OK,"TEST")
            dialog1.run()
            dialog1.destroy()


    class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
        window = GObject.property(type=Gedit.Window)
        def __init__(self):
            GObject.Object.__init__(self)
            self.instances = {}

        def do_activate(self):
            self.instances[self.window] = test(self, self.window)

        def do_deactivate(self):
            if self.window in self.instances:
                self.instances[self.window].deactivate()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接