14.5. TreeViewColumns

14.5.1. Creating TreeViewColumns

A TreeViewColumn is created using the constructor:

  treeviewcolumn = gtk.TreeViewColumn(title=None, cell_renderer=None, ...)

where title is the string to be used as the column header label, and cell_renderer is the first CellRenderer to pack in the column. Additional arguments that are passed to the constructor are keyword values (in the format attribute=column) that set attributes on cell_renderer. For example:

  treeviewcolumn = gtk.TreeViewColumn('States', cell, text=0, foreground=1)

creates a TreeViewColumn with the CellRendererText cell retrieving its text from the first column of the tree model and the text color from the second column.

14.5.2. Managing CellRenderers

A CellRenderer can be added to a TreeViewColumn using one of the methods:

  treeviewcolumn.pack_start(cell, expand)
  treeviewcolumn.pack_end(cell, expand)

pack_start() and pack_end() add cell to the start or end, respectively, of the TreeViewColumn. If expand is TRUE, cell will share in any available extra space allocated by the TreeViewColumn.

The get_cell_renderers() method:

  cell_list = treeviewcolumn.get_cell_renderers()

returns a list of all the CellRenderers in the column.

The clear() method removes all the CellRenderer attributes from the TreeViewColumn:

  treeviewcolumn.clear()

There are a large number of other methods available for a TreeViewColumn - mostly dealing with setting and getting properties. See the PyGTK Reference Manual for more information on the TreeViewColumn properties. The capability of using the built-in sorting facility is set using the method:

  treeviewcolumn.set_sort_column_id(sort_column_id)

sets sort_column_id as the tree model sort column ID to use when sorting the TreeView display. This method also sets the "clickable" property of the column that allows the user to click on the column header to activate the sorting. When the user clicks on the column header, the TreeViewColumn sort column ID is set as the TreeModel sort column ID and the TreeModel rows are resorted using the associated sort comparison function. The automatic sorting facility also toggles the sort order of the column and manages the display of the sort indicator. See Section 14.2.9, “Sorting TreeModel Rows” for more information on sort column IDs and sort comparison functions. Typically when using a ListStore or TreeStore the default sort column ID (i.e. the column index) of the TreeModel column associated with the TreeViewColumn is set as the TreeViewColumn sort column ID.

If you use the TreeViewColumn headers for sorting by using the set_sort_column_id() method, you don't need to use the TreeSortable set_sort_column_id() method.

You can track the sorting operations or use a header click for your own purposes by connecting to the "clicked" signal of the TreeView column. The callback function should be defined as:

  def callback(treeviewcolumn, user_data, ...)