PyGtk3 右键单击​​树视图上的菜单和操作

PyGtk3 right click menu on treeview with actions

我想使用操作向 TreeView 小部件添加右键单击时的上下文菜单。我通过混合来自 http://python-gtk-3-tutorial.readthedocs.io/en/latest/application.html#actions and https://developer.gnome.org/gnome-devel-demos/stable/gmenu.py.html 的信息来尝试此代码,但它不起作用(NotImplementedError:无法构造 ActionGroup)

action = Gio.SimpleAction.new("test", None)
action.connect('activate', self.my_func)
self.add_action(action)

self.tree_view.connect("button-press-event", self.on_click)

def on_click(self, widget, event):
        if event.button == 3:
            path, _, _, _ = widget.get_path_at_pos(int(event.x), int(event.y))
            treeiter = self.model.get_iter(path)
            action_group = Gio.ActionGroup()
            action_group.action_added("app.test")
            treeiter.insert_action_group(action_group)
            menu = Gtk.Menu()
            menu.attach_to_widget(treeiter)
            menu.popup()

Gio.ActionGroup是抽象接口,不能直接构造。相反,您想要 Gio.SimpleActionGroup 及其 insert() 方法而不是 action_added.