Satya's blog - Treeviews in PyGTk
Let's say you want to display a nice list of cities, grouped by state, like so:
In a GUI window of a Python PyGTk app, of course. Let's also say that the list will have that little collapsing triangle next to each state, so you can close and open the sub-list of cities. As a bonus, let's make the columns sortable. What you need is GtkTreeView. What you want is something simpler. Sucks to be you. Assuming you know how to do the rest, let's concentrate on the TreeView itself. It's sort-of MVC-ish, so your TreeView needs a TreeModel or rather, a TreeStore: treestore=gtk.TreeStore(str,str) for state in states: piter=treestore.append(None, [state['name'], None]) for city in states['cities']: treestore.append(piter, [None, city])
So, we instantiate a treestore object and tell it that we want two string
columns.
Next we loop through the list of states. Each item is a dictionary, the name
and the cities list. The line that starts Now we set up a sortable tree model using this treestore, and a treeview using the sortable tree model: tmsort = gtk.TreeModelSort(treestore) # produce a sortable treemodel treeview = gtk.TreeView(tmsort) # the tree view Now, for each column (State and City), we do the following:
The last two lines make the column searchable and sortable. tvcolumns={} # the columns cells={} # the cells i=0 for c in ('State','City'): # the actual column headers # instantiate TVC tvcolumns[c] = gtk.TreeViewColumn(c) # add to the treeview treeview.append_column(tvcolumns[c]) # instantiate and add the cell object cells[c]=gtk.CellRendererText() tvcolumns[c].pack_start(cells[c], True) #add the cell to the column, allow it to expand # now set the cell's text attribute to the treeview's column i (0,1) tvcolumns[c].add_attribute(cells[c], 'text', i) #make it searchable and sortable treeview.set_search_column(i) tvcolumns[c].set_sort_column_id(i) i+=1 Whee! Now we just need to put this in a scrolled window in case it's too big for our containing widget: treeview.show() scrolled_window = gtk.ScrolledWindow() scrolled_window.add_with_viewport(treeview) scrolled_window.set_size_request(300,200) scrolled_window.show()
Suppose we want to do something, like open a window showing the city weather,
when a city is double-clicked. Let us connect a signal handler to the treeview
(I believe it might be easier to connect one to each row or cell, but less
efficient). The signal we want to look for is treeview.connect("row-activated", self.city_clicked, None) And that's it. That should show our nice grouped table when run (and added to a main window, etc).
But the most confusing and poorly documented bit for
me was the signal handler, def city_clicked(self, treeview, iter, tvc, foo): model=treeview.get_model() iter = model.get_iter(iter) city_name = model.get_value(iter, 1) # column id 1 contains the city name The first parameter to city_clicked is self, because we're in a class, remember? The next two are the treeview object and the iter object that was activated. Next is tvc, the TreeViewColumn object for the cell that was clicked. The last one, foo, is the extra data, in this case None.
So we get the TreeView's underlying model. The I should write a wrapper class for this stuff. It's way too much work as it stands. Update: Or, you can use glade. It's a user interface layout designer thingy. See http://www.learningpython.com/...g-pygtk-and-glade/ On the other hand, all that does is build the interface, it will NOT populate your treeview. That's not a UI function -- not in the view, in MVC terms -- it's in the Model or Controller, i.e. in your python code, as above. Last updated: Oct 13 2007 10:36 |
|