PyGtk3右键单击树视图上的菜单并执行操作

时间:2016-10-09 18:46:01

标签: python menu action pygtk gtk3

我想使用Actions在右键单击上添加一个上下文菜单到TreeView小部件。我通过混合来自http://python-gtk-3-tutorial.readthedocs.io/en/latest/application.html#actionshttps://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()

1 个答案:

答案 0 :(得分:1)

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

相关问题