gpt4 book ai didi

python - Accel 在 gedit 3 插件中不起作用

转载 作者:行者123 更新时间:2023-11-28 17:42:15 30 4
gpt4 key购买 nike

我正在尝试为 Gedit 3 编写一个使用 GObject 自省(introspection)的小插件。下面显示的代码的相关部分旨在建立一个环境,然后我可以在该环境中适应按钮回调中的函数。但是,按钮的加速器不起作用。

  1. 这段代码有什么问题?
  2. 我正在使用教程 here和 GTK 3 的 python 文档 here .还有其他您知道的链接吗?

    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()

最佳答案

知道 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()

关于python - Accel 在 gedit 3 插件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22594173/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com