blob: 2a8da2b6c9ca2314c23afbfe8e6c83e44253f19f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# [SNIPPET_NAME: Basic Tree View]
# [SNIPPET_CATEGORIES: PyGTK]
# [SNIPPET_DESCRIPTION: Create a basic tree view]
# [SNIPPET_DOCS: http://www.pygtk.org/docs/pygtk/class-gtktreeview.html, http://www.pygtk.org/pygtk2tutorial/ch-TreeViewWidget.html]
# example basictreeview.py
import pygtk
pygtk.require('2.0')
import gtk
class TreeView(gtk.TreeView):
def __init__(self):
gtk.TreeView.__init__(self)
# create a TreeStore with one string column to use as the model
self.treestore = gtk.TreeStore(str)
# we'll add some data now - 4 rows with 3 child rows each
for parent in range(4):
piter = self.treestore.append(None, ['parent %i' % parent])
for child in range(3):
self.treestore.append(piter, ['child %i of parent %i' %
(child, parent)])
# create the TreeView using treestore
self.set_model(self.treestore)
# create the TreeViewColumn to display the data
self.tvcolumn = gtk.TreeViewColumn('Column 0')
# add tvcolumn to treeview
self.append_column(self.tvcolumn)
# create a CellRendererText to render the data
self.cell = gtk.CellRendererText()
# add the cell to the tvcolumn and allow it to expand
self.tvcolumn.pack_start(self.cell, True)
# set the cell "text" attribute to column 0 - retrieve text
# from that column in treestore
self.tvcolumn.add_attribute(self.cell, 'text', 0)
# make it searchable
self.set_search_column(0)
# Allow sorting on the column
self.tvcolumn.set_sort_column_id(0)
# Allow drag and drop reordering of rows
self.set_reorderable(True)
self.show_all()
|