Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've been working on a minor (first time) app with quickly and hit a hurdle - how do I get the selected row (the data) from a TreeView? The data to the TreeView is passed from a list of files in a directory, and I need to know which rows were selected (and thus which files were). What is the best way to do that?

Here's the current code:

    self.treeview = self.builder.get_object("treeview")
    select = self.treeview.get_selection()
    select.connect("changed", self.on_tree_selection_changed)

def on_tree_selection_changed(selection):
    model, treeiter = self.treeview.selection-get()
    if treeiter != None:
        print "You selected", model[treeiter][0]
share|improve this question
    
The code above won't work because the signal passes the class instance (self) as the first argument to the function. Also the function call is incorrect. See my answer below –  Ian B. Oct 10 '12 at 16:55

1 Answer 1

up vote 1 down vote accepted

See here: http://python-gtk-3-tutorial.readthedocs.org/en/latest/treeview.html

You can get the selection on an event by connecting a selection object to an event:

select = tree.get_selection()
select.connect("changed", on_tree_selection_changed)
def on_tree_selection_changed(selection):
    model, treeiter = selection.get_selected()
    if treeiter != None:
        print "You selected", model[treeiter][0]

Edit: To set this up within a window class, use the following code:

class MyTreeView....
    ....Other Stuff...
        self.treeview = self.builder.get_object("treeview")
        select = self.treeview.get_selection()
        select.connect("changed", self.on_tree_selection_changed)

    def on_tree_selection_changed(self, selection):
        model, treeiter = selection.get_selected()
        if treeiter != None:
            print "You selected", model[treeiter][0]

Edit2: If you want to have multiple rows selected you need to use Gtk.TreeSelection.get_selected_rows() which returns a list of Gtk.TreePath instances of all selected rows.

share|improve this answer
    
Hm, I used this, but what I get every time something is seleceted is: "TypeError: on_tree_selection_changed() takes exactly 1 argument (2 given)". –  GoGoDo Oct 10 '12 at 4:05
1  
Edit your question with the relevant code that fails. –  Timo Oct 10 '12 at 8:54
    
Try putting in an extra argument in the function definition( def on_tree_selection_changed(selection,data):) Then in the function print out a representation of what each is to see (eg: print repr(selection) or print repr(data)). One of them should be the correct data structure (ie the Gtk.TreeSelection) to use. Posting the code would help too. –  Ian B. Oct 10 '12 at 14:05
    
I added the code to my question. –  GoGoDo Oct 10 '12 at 15:47
    
Thanks! But this now results in a different error: "Gtk-CRITICAL **: gtk_tree_selection_get_selected: assertion `priv->type != GTK_SELECTION_MULTIPLE' failed". This works when I set it to single select, so I guess multi-select treeviews require different stuff. Sorry for not saying that in my post. –  GoGoDo Oct 10 '12 at 17:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.