PyGTK TreeView Class - PyGTK

What is PyGTK TreeView Class?

The Treeview widget shows contents of a model applying the gtk.TreeModel interface. PyGTK offers the resulting types of models −

  • gtk.ListStore
  • gtk.TreeStore
  • gtk.TreeModelSort

ListStore is a list model. When related with a gtk.TreeView widget, it creates a List box covering the items to be selected from. A gtk.ListStore object is declared with resulting syntax –

A list may have multiple columns, the predefined type constants are −

  • gobject.TYPE_BOOLEAN
  • gobject.TYPE_BOXED
  • gobject.TYPE_CHAR
  • gobject.TYPE_DOUBLE
  • gobject.TYPE_ENUM
  • gobject.TYPE_FLOAT
  • gobject.TYPE_INT
  • gobject.TYPE_LONG
  • gobject.TYPE_NONE
  • gobject.TYPE_OBJECT
  • gobject.TYPE_STRING
  • gobject.TYPE_UCHAR
  • gobject.TYPE_UINT
  • gobject.TYPE_ULONG
  • gtk.gdk.pixbuf etc.

For instance, a ListStore object to store string items is declared as –

In order to add items in the store, append() methods are used –

TreeStore is a model for multi-columned Tree widget. For instance, the resulting declaration makes a store with one column having string item.

In order to add items in a TreeStore, use the append() method. The append() method has two parameters, parent and row. To add toplevel item, parent is None.

You must repeat this statement to add multiple rows.

In order to add child rows, pass the toplevel row as parent parameter to the append() method –

You must repeat this statement to add multiple child rows.

Currently, make a TreeView widget and use the above TreeStore object as model.

We currently have to create TreeViewColumn to show store data. The object of gtk.TreeViewColumn manages header and the cells using gtk.CelRenderer. TreeViewColumn object is created using the resulting constructor –

In addition to title and renderer, it takes zero or more attribute=column pairs to state from which tree model column the attribute's value is to be retrieved. These parameters can too be set using methods of TreeViewColumn class given below.

A gtk.CellRenderer is a base class for a set of objects for rendering different types of data. The derived classes are CellRendererText, CellRendererPixBuf and CellRendererToggle.

The resulting methods of the TreeViewColumn class are used to configure its object −

  • TreeViewColumn.pack_start(cell, expand = True) − This method packs the CellRenderer object into the beginning column. If expand parameter is set to True, columns entire allocated space is assigned to cell.
  • TreeViewColumn.add_attribute(cell, attribute, column) − This method adds an attribute mapping to the list in the tree column. The columnis the column of the tree model.
  • TreeViewColumn.set_attributes() − This method sets the attribute locations of the renderer using the attribute = column pairs
  • TreeViewColumn.set_visible() − If True, the treeview column is visible
  • TreeViewColumn.set_title() − This method sets the "title" property to the value stated.
  • TreeViewColumn.set_lickable() − If set to True, the header can take keyboard focus, and be clicked.
  • TreeViewColumn.set_alignment(xalign) − This method sets the "alignment" property to the value of xalign.

The "clicked" signal is emitted when the user clicks on the treeviewcolumnheader button.

After having configured the TreeViewColumn object, it is added to the TreeView widget using the append_column() method.

The resulting are the vital methods of the TreeView class −

  • TreevVew.set_model() − This sets the "model" property for the treeview. If the treeview already has a model set, this method will remove it before setting the new model. If model is None, it will unset the old model.
  • TreeView.set_header_clickable() − If set to True, the column title buttons can be clicked.
  • TreeView.append_column() − This appends the stated TreeViewColumn to the list of columns.
  • TreeView.remove_column() − This removes the stated column from the treeview.
  • TreeView.insert_column() − This inserts the stated column into the treeview at the location stated by position.

The TreeView widget emits the resulting signals −


This is emitted when the cursor moves or is set.


This is emitted when the row at the cursor needs to be expanded or collapsed.


This is emitted when the user double clicks atreeviewrow


This is emitted when a row is collapsed by the user or programmatic action.


This is emitted when a row is expanded via the user or programmatic action.

Two instances of the TreeView widget are given below. The first instance uses a ListStore to produce a simple ListView.

Now a ListStore object is shaped and string items are added to it. This ListStore object is used as model for TreeView object –

Then a CellRendererText is added to a TreeViewColumn object and the same is appended to TreeView.

TreeView Object is placed on the toplevel window by adding it to a Fixed container.

Example 1

Observe the resulting code –

The item selected by the user is showed on a label in the window as the on_activated callback function is invoked.

Example 2

The second instance builds a hierarchical TreeView from a TreeStore. This program follows the same sequence of building the store, setting it as model for TreeView, designing a TreeViewColumn and appending it to TreeView.

The resulting TreeView is showed as an output –


All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

PyGTK Topics