我想使用Actions在右键单击上添加一个上下文菜单到TreeView小部件。我通过混合来自http://python-gtk-3-tutorial.readthedocs.io/en/latest/application.html#actions和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()
答案 0 :(得分:1)
Gio.ActionGroup
是一个抽象接口,不能直接构造。相反,您需要Gio.SimpleActionGroup
及其insert()
方法,而不是action_added
。