[pygtk] treestore problems

Timo timomlists at gmail.com
Tue Feb 1 01:26:14 WST 2011


On 31-01-11 16:49, Bevis Peters wrote:
> I have created a simple treestore, with each node containing two strings, ie:
>
>     self.wTreeModel = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
>
> and allowed multiple selection with:
>
>     self.wTreeSelection = self.wTreeView.get_selection()
>     self.wTreeSelection.set_mode(gtk.SELECTION_MULTIPLE)
>
> and then populated it with something like:
>
>     child = self.wTreeModel.append(parent, [<a string to display on
> the tree>,<some data associated with that row>  ])
>
> all well and good; the tree shows up correctly in the window.
>
> But now I want to be able to select one or more of the rows in the
> tree, then get back the<some data>  associated with each row.  I have
> tried something like:
>
>          treeSelection = self.wTreeView.get_selection()
>          model, selected = treeSelection.get_selected_rows()
>          for path in selected:
>              for i in range(len(path)):
>                  it = model.get_iter(path[i])
>                  print model[it][1]
There's something wrong with how you get the data here. You pass an iter 
where you need a path.

Try this:
for path in selected:
     print model[path][1]

A path is a row, so for every row in the selected rows, get the row in 
the model (model[path]) and retrieve the data from the second column 
([1], zero-based).

To get the data with an iter, you could do this:
for path in selected:
     rowiter = model.get_iter(path)
     print model.get_value(rowiter, 1)

This is more useful when you have a single selection treeview and use 
get_selected(), because that will return an iter of its own.
model, rowiter = selection.get_selected()
data = model.get_value(rowiter, 1)

Cheers,
Timo

> which I feel should print the<some data>  for each selected row, but
> what actually comes out seems largely unrelated to what has been
> selected.  So for instance, given a tree:
>
> one --
>        |-- two --
>                  |-- three
>
> I should be able to select "three" and get back the data associated
> with it.  What I actually get back from the above bit of code is the
> data associated with "one", three times.
>
> Any help would be greatly appreciated!
>
>
> bevis
> _______________________________________________
> pygtk mailing list   pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/



More information about the pygtk mailing list