Python : Get gtk.treeview selection from another widget

不羁岁月 提交于 2020-02-05 15:50:31

问题


It looks like the only way to get the selected item of a gtk.TreeView() is to actually click on it :

    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?


回答1:


To get the selection of a tree view, call the get_selected_rows method of the gtk.TreeSelection object. 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. 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:

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:

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.



来源:https://stackoverflow.com/questions/18160479/python-get-gtk-treeview-selection-from-another-widget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!