Each row in the list can be separated into multiple columns, and rows in the list can contain child rows to create an expandable-list, or tree. The TreeView can be extremely intimidating at first, but it is extremely powerful - especially when compared to the ListBox and ListView controls from the Windows Forms toolkit. Components of the TreeView are separated into these three categories: The Model, which stores data to be displayed, the View, which controls how to display the data, and the controller, which controls what data is displayed, how it is sorted, etc. Model The TreeView has two basic models: ListStore, which stores a flat set of data, and TreeStore, which stores data that can contain sub-items used for creating a Tree.
|Published (Last):||28 May 2018|
|PDF File Size:||8.20 Mb|
|ePub File Size:||18.57 Mb|
|Price:||Free* [*Free Regsitration Required]|
Edit on GitHub TreeView and its associated widgets are an extremely powerful way of displaying data. They are used in conjunction with a Gtk. ListStore or Gtk. TreeStore and provide a way of displaying and manipulating data in many ways, including: Automatic updates when data is added, removed or edited Drag and drop support Embedding widgets such as check boxes, progress bars, etc. Reorderable and resizable columns Filtering data With the power and flexibility of a Gtk.
TreeView comes complexity. It is often difficult for beginner developers to be able to utilize it correctly due to the number of methods which are required. TreeView has an associated Gtk. TreeModel , which contains the data displayed by the TreeView. Each Gtk. TreeModel can be used by more than one Gtk.
For instance, this allows the same underlying data to be displayed and edited in 2 different ways at the same time. Although you can theoretically implement your own Model, you will normally use either the Gtk. TreeStore model classes. ListStore contains simple rows of data, and each row has no children, whereas Gtk. TreeStore contains rows of data, and each row may have child rows.
When constructing a model you have to specify the data types for each column the model holds. ListStore str, str, float This creates a list store with three columns, two string columns, and a float column. Adding data to the model is done using Gtk. Knuth", TreeIter instance, which points to the location of the newly inserted row. You can retrieve a Gtk. TreeIter by calling Gtk. Once data has been inserted, you can retrieve or modify data using the tree iter and column index.
TreeStore , the above code will only iterate over the rows of the top level, but not the children of the nodes. TreeModel with the list-like method mentioned above, it is also possible to either use Gtk. TreeIter or Gtk. TreePath instances. Both reference a particular row in a tree model. One can convert a path to an iterator by calling Gtk.
As Gtk. ListStore contains only one level, i. TreeStore , a path is a list of indexes or a string. The string form is a list of numbers separated by a colon.
Each number refers to the offset at that level. TreePath can be accessed like lists, i. It works with either the list or the tree store.
Setting up a Gtk. TreeView is not a difficult matter. It needs a Gtk. TreeModel to know where to retrieve its data from, either by passing it to the Gtk. TreeView constructor, or by calling Gtk. TreeView store Once the Gtk. TreeView widget has a model, it will need to know how to display the model. It does this with columns and cell renderers. Cell renderers are used to draw the data in the tree model in a way. CellRendererText , Gtk. CellRendererPixbuf and Gtk. In addition, it is relatively easy to write a custom renderer yourself.
A Gtk. TreeViewColumn is the object that Gtk. TreeView uses to organize the vertical columns in the tree view. It needs to know the name of the column to label for the user, what type of cell renderer to use, and which piece of data to retrieve from the model for a given row. TreeViewColumn instance and use Gtk. CellRendererText column. TreeStore and Gtk. ListStore , which implement the Gtk. TreeSortable interface. TreeView can easily made sortable with a call to Gtk. Afterwards the column can be sorted by clicking on its header.
First we need a simple Gtk. TreeView and a Gtk. ListStore as a model. ListStore str model. As an example we will create a comparison function that sorts case-sensitive. In the example above the sorted list looked like: alfred Alfred benjamin Benjamin charles Charles david David The case-sensitive sorted list will look like: Alfred Benjamin Charles David alfred benjamin charles david First of all a comparison function is needed.
This function gets two rows and has to return a negative integer if the first one should come before the second one, zero if they are equal and a positive integer if the second one should come before the second one. TreeModelFilter class. This class, like Gtk. ListStore , is a Gtk. TreeStore or a Gtk. ListStore , hiding some elements to the view. In practice, it supplies the Gtk. TreeView with a subset of the underlying model.
Instances of Gtk. They can also be chained with Gtk. TreeModelSort instances. You can create a new instance of a Gtk. TreeModelFilter and give it a model to filter, but the easiest way is to spawn it directly from the filtered model, using the Gtk. ListStore - Gtk. TreeModelFilter - Gtk. TreeView stack. Grid self.
ScrolledWindow self. RIGHT, 1, 1 self.
PyGTK - TreeView Class
ListStore gobject. For example, the following statement creates a store with one column having string item. TreeStore gobject. The append method has two parameters, parent and row. To add toplevel item, parent is None. Now, create a TreeView widget and use the above TreeStore object as model. The object of gtk.
Editable Cells 9. For instance, strings and numbers are shown in a simple Gtk::Entry widget, and booleans are shown in a Gtk::CheckButton. This is usually what you need. Note that unsigned short is not supported by default - You could use unsigned int or unsigned long as the column type instead. Specifying CellRenderer details The default CellRenderers and their default behaviour will normally suffice, but you might occasionally need finer control. Implementing custom logic for editable cells.
GtkSharp TreeView Tutorial