Python-Ref > GUI programming with PyGTK > Widgets > TreeView II. - available cell renderers
 
 

<-^^->

TreeView II. - available cell renderers

What kinds of cell renderers are available.
Expand/Shrink
  1   # ensure that PyGTK 2.0 is loaded - not an older version
  2   import pygtk
  3   pygtk.require('2.0')
  4   # import the GTK module
  5   import gtk
  6   import gobject
  7   
  8   class MyGUI:
  9   
 10     def __init__( self, title):
 11       self.window = gtk.Window()
 12       self.title = title
 13       self.window.set_title( title)
 14       self.window.set_size_request( -1, -1)
 15       self.window.connect( "destroy", self.destroy)
 16       self.create_interior()
 17       self.window.show_all()
 18   
 19     def create_interior( self):
 20       self.mainbox = gtk.ScrolledWindow()
 21       self.mainbox.set_policy( gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
 22       self.window.add( self.mainbox)
 23       # model creation
 24       # URL, recursion_depth, download, processed part, what to download
 25       self.model = gtk.ListStore( str, int, bool, float, str) 
 26       for url,download in (["http://root.cz",True],
 27                            ["http://slashdot.org",True],
 28                            ["http://mozilla.org",False]):
 29         adj = gtk.Adjustment( value=0, lower=0, upper=10, step_incr=1)
 30         self.model.append( [url, 0, download, 0.0, "HTML only"])
 31       # the treeview
 32       treeview = gtk.TreeView( self.model) 
 33       # individual columns
 34       # URL column
 35       col = gtk.TreeViewColumn( "URL")
 36       treeview.append_column( col)
 37       cell = gtk.CellRendererText()
 38       col.pack_start( cell, expand=False)
 39       col.set_attributes( cell, text=0)
 40       col.set_sort_column_id( 0)
 41       # recursion_depth column
 42       col = gtk.TreeViewColumn( "Depth")
 43       treeview.append_column( col)
 44       cell = gtk.CellRendererSpin()
 45       cell.set_property( "adjustment", gtk.Adjustment( lower=0, upper=10, step_incr=1))
 46       cell.set_property( "editable", True)
 47       col.pack_start( cell, expand=False)
 48       col.set_attributes( cell, text=1)
 49       cell.connect('edited', self._recursion_depth_changed, 1)
 50       # State
 51       col = gtk.TreeViewColumn( "State")
 52       treeview.insert_column( col, 0)
 53       cell = gtk.CellRendererPixbuf()
 54       col.pack_start( cell, expand=False)
 55       col.set_cell_data_func( cell, self._render_icon)
 56       # Download column
 57       col = gtk.TreeViewColumn( "Download")
 58       treeview.append_column( col)
 59       cell = gtk.CellRendererToggle()
 60       cell.set_property( "activatable", True)
 61       col.pack_start( cell, expand=False)
 62       col.set_attributes( cell, active=2)
 63       col.set_sort_column_id( 2)
 64       cell.connect('toggled', self._download_toggled, 2)
 65       # What to download column
 66       col = gtk.TreeViewColumn( "What")
 67       treeview.append_column( col)
 68       cell = gtk.CellRendererCombo()
 69       cell.set_property( "editable", True)
 70       # we need a separate model for the combo
 71       model = gtk.ListStore( str)
 72       for value in ["HTML only", "HTML+CSS", "Everything"]:
 73         model.append( [value])
 74       cell.set_property( "model", model)
 75       cell.set_property( "text-column", 0)
 76       col.pack_start( cell, expand=False)
 77       col.set_attributes( cell, text=4)
 78       col.set_sort_column_id( 4)
 79       cell.connect('edited', self._what_to_download_changed, 4)
 80       # Progress column
 81       col = gtk.TreeViewColumn( "Progress")
 82       treeview.append_column( col)
 83       cell = gtk.CellRendererProgress()
 84       col.pack_start( cell, expand=True)
 85       col.set_attributes( cell, value=3)
 86       col.set_sort_column_id( 3)
 87       # pack the treeview
 88       self.mainbox.add( treeview)
 89       # show the box
 90       self.mainbox.set_size_request( 500, 260)
 91       self.mainbox.show()
 92       # set up timer
 93       self._timer = gobject.timeout_add( 200, self._timeout) 
 94   
 95     def _render_icon( self, column, cell, model, iter):
 96       data = self.model.get_value( iter, 2)
 97       if data == True:
 98         stock = gtk.STOCK_YES
 99       else:
100         stock = gtk.STOCK_NO
101       pixbuf = self.window.render_icon( stock_id=stock, size=gtk.ICON_SIZE_MENU)
102       cell.set_property( 'pixbuf', pixbuf)
103   
104     def _download_toggled( self, w, row, column):
105       self.model[row][column] = not self.model[row][column]
106   
107     def _recursion_depth_changed( self, w, row, new_value, column):
108       self.model[row][column] = int( new_value)
109   
110     def _what_to_download_changed( self, w, row, new_value, column):
111       self.model[row][column] = new_value
112   
113     def _timeout( self):
114       for row in self.model:
115         if row[2] and row[3] < 100:
116           # this row is downloadable
117           row[3] += 3
118           if row[3] > 100:
119             row[3] = 100
120       return True
121         
122   
123     def main( self):
124       gtk.main()
125   
126     def destroy( self, w):
127       gobject.source_remove(self._timer)
128       gtk.main_quit()
129   
130   
131   if __name__ == "__main__":
132     m = MyGUI( "TreeView example II.")
133     m.main()
Screenshot:
Program screenshot gtk-treeview2-1.png
Doba běhu: 1507.2 ms