简体   繁体   English

Python:从另一个小部件获取gtk.treeview选择

[英]Python : Get gtk.treeview selection from another widget

It looks like the only way to get the selected item of a gtk.TreeView() is to actually click on it : 看来获取gtk.TreeView()的选定项目的唯一方法是实际单击它:

    tree_selection = self.treeview.get_selection()
    tree_selection.connect('changed', self.my_callback)
    self.treeview.connect('row-activated', self.my_other_callback)

But what if I'm listing files in my treeview, and need a "file properties" menu item? 但是,如果我要在树形视图中列出文件,并且需要“文件属性”菜单项怎么办? Or a play button, that needs to access selected file to pass the filename to a player class / method ? 还是需要访问选定文件以将文件名传递给播放器类/方法的播放按钮?

Bonus question : How to call my_other_callback from tree_selection.connect('changed', ...) (that does not seem to return all the row data..?) or in other words, how to pass treeview and path to the callback? 额外的问题:如何从tree_selection.connect('changed', ...)调用my_other_callback tree_selection.connect('changed', ...)似乎并没有返回所有行数据..?),或者换句话说,如何将treeviewpath传递给回调?

To get the selection of a tree view, call the get_selected_rows method of the gtk.TreeSelection object. 要选择树形视图,请调用gtk.TreeSelection对象get_selected_rows方法 You can call it at any place from which you can access the tree view. 您可以在任何可以访问树状视图的位置调用它。

It is unclear why you want to pass the tree view to my_other_callback since it, being a method on your class, can access it as self.treeview . 目前尚不清楚为什么要将树视图传递给my_other_callback因为它作为类的一种方法可以作为self.treeview来访问它。 But if you want to do it anyway, you can add the tree view (or any other Python object) as an additional argument to connect : 但是,无论如何,您都可以将树视图(或任何其他Python对象)添加为connect的附加参数:

tree_selection.connect('changed', self.my_other_callback, self.treeview)

For an even finer-grained control of how the callback is invoked, use a lambda: 要更精细地控制回调的调用方式,请使用lambda:

tree_selection.connect('changed', lambda *args: self.my_other_callback(self.treeview))

This allows you to use the same handler for multiple signals without having to declare the handler as accepting *args . 这使您可以将同一处理程序用于多个信号,而不必将处理程序声明为接受*args

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM