Python Gtk HOWTO: cell rendering a Treeview created in Glade

Filed under: Tutorials and HOWTOs by Hari
Posted at 14:11 IST (last updated: 25 May 2010 @ 08:25 IST)
In this series Next >
This is more for personal reference after struggling for half an hour with Python and Glade/GtkBuilder to figure out how to add a gtk.CellRenderer to a gtk.TreeViewColumn for a tree view that was created in Glade.

The steps are not obvious because most of the tutorials out there are for pure GTK code and assumes all the objects are created programmatically and not from the GtkBuilder XML file.

Simply the steps are:
  • Add a column to the TreeView in Glade. This is done by editing the treeview not the tree model object. Right click on the tree view object and click on Edit... in the context menu. Then add a column from the Hierarchy tab.
  • Now associate a ListStore or any other tree model with the tree view. This can be done either in Glade or programmatically. Take your pick. I prefer to do it in Glade because you can create columns for the tree model as well.
  • Now in the Glade program add a cell renderer to the column and associate it with the column number in the model as follows. Place something like the following piece of code somewhere in your initialization code (obviously the variable names/attributes will have to change

# Get the TreeViewColumn object associated with the tree view - self.ui is a gtk.Builder object
# in this case the index is 0
col = self.ui.get_object ("tree_view").get_column (0)
# Create a text cell renderer
cell = gtk.CellRendererText ()
# Add the cell renderer to the TreeViewColumn
col.pack_start (cell)
# Attributes for the column - make it display text of column 0
# from the model
col.add_attribute (cell, "text", 0)

It's all a bit confusing and irritating to have to deal with all this and I personally think that GTK's treeview control is a royal pain in the rear for programmers, especially when used in conjunction with Glade.

Half the stuff is done in the Glade interface and the rest programmatically. This is a mess and should be a high priority for the GTK programmers to rectify: either improve Glade or simplify gtk.TreeView.

In this series

1 comment(s)

Leave a comment »
  1. You can add the cell renderer for the tree view in the Glade. We can add cell renderer for the GtkComboBox directly in the Glade. We can't edit it for the GtkTreeView directly. But we can copy the source code of the GtkComboBox's cell render to the GtkTreeView and the Glade recognized them.

    Comment by xingmin (visitor) on 29 Aug 2010 @ 06:46 IST #

Leave a comment

First-time comments on this blog are moderated.
Your name*
Email ID*
(wont' be published)
Website
Your comments*
(No HTML allowed)
:-) :-D :biggrin: :-P ;-) 8-) :-( :mad: |-| :oops: :-/ :-| :roll:
bold italic quote code
Code* captcha Enter the code you see in the image
* required fields