Python-Ref > GUI programming with PyGTK > Widgets > TreeView III. - advanced cell renderer topics
 
 

<-^^->

TreeView III. - advanced cell renderer topics

What can we do with cell renderers.
The following example demonstrates some interesting used of cell renderers, such as using more than one model column in one cell-renderer, etc.
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       # text, color, editable
 25       self.model = gtk.ListStore( str, str, bool)
 26       self.model.append( ["Editable and red","#FF0000",True])
 27       self.model.append( ["Noneditable","#FFFFFF",False])
 28       self.model.append( ["Noneditable and green","#00FF00",False])
 29       # the treeview
 30       treeview = gtk.TreeView( self.model) 
 31       # individual columns
 32       # Text column
 33       col = gtk.TreeViewColumn( "Text")
 34       treeview.append_column( col)
 35       cell = gtk.CellRendererText()
 36       col.pack_start( cell, expand=False)
 37       col.set_attributes( cell, text=0, cell_background=1, editable=2) 
 38       cell.connect('edited', self._text_changed, 0)
 39       col.set_sort_column_id( 0)
 40       # Editable column
 41       col = gtk.TreeViewColumn( "Editable")
 42       treeview.append_column( col)
 43       cell = gtk.CellRendererToggle()
 44       cell.set_property( "activatable", True)
 45       col.pack_start( cell, expand=False)
 46       col.set_attributes( cell, active=2, cell_background=1)
 47       col.set_sort_column_id( 2)
 48       cell.connect('toggled', self._editable_toggled, 2)
 49       # pack the mainbox
 50       self.mainbox.add( treeview)
 51       # show the box
 52       self.mainbox.set_size_request( 260, 200)
 53       self.mainbox.show()
 54   
 55     def _text_changed( self, w, row, new_value, column):
 56       self.model[row][column] = new_value
 57   
 58     def _editable_toggled( self, w, row, column):
 59       self.model[row][column] = not self.model[row][column]
 60   
 61     def main( self):
 62       gtk.main()
 63   
 64     def destroy( self, w):
 65       gtk.main_quit()
 66   
 67   
 68   if __name__ == "__main__":
 69     m = MyGUI( "TreeView example III.")
 70     m.main()
Screenshot:
Program screenshot gtk-treeview3-1.pngProgram screenshot gtk-treeview3-1a.png
Doba běhu: 625.1 ms
Now a colored version of the exchange rate example from TreeView.
Expand/Shrink
Country|Currency|Amount|Code|Rate
Australia|dollar|1|AUD|14.774
Brazil|real|1|BRL|9.370
Bulgaria|lev|1|BGN|12.855
Canada|dollar|1|CAD|15.550
China|renminbi|1|CNY|2.265
Croatia|kuna|1|HRK|3.461
Denmark|krone|1|DKK|3.370
EMU|euro|1|EUR|25.145
Estonia|kroon|1|EEK|1.607
Hongkong|dollar|1|HKD|2.034
Hungary|forint|100|HUF|9.922
Iceland|krona|100|ISK|21.759
IMF|SDR|1|XDR|26.052
Indonesia|rupiah|1000|IDR|1.723
Japan|yen|100|JPY|15.798
Latvia|lats|1|LVL|36.092
Lithuania|litas|1|LTL|7.283
Malaysia|ringgit|1|MYR|5.026
Mexico|peso|1|MXN|1.498
New Zealand|dollar|1|NZD|12.671
Norway|krone|1|NOK|3.172
Philippines|peso|100|PHP|38.121
Poland|zloty|1|PLN|7.305
Romania|new leu|1|RON|6.854
Russia|rouble|100|RUB|67.640
Singapore|dollar|1|SGD|11.675
Slovakia|koruna|100|SKK|77.734
South Africa|rand|1|ZAR|2.004
South Korea|won|100|KRW|1.622
Sweden|krona|1|SEK|2.679
Switzerland|franc|1|CHF|15.978
Thailand|baht|100|THB|50.236
Turkey|new lira|1|TRY|12.057
United Kingdom|pound|1|GBP|31.394
USA|dollar|1|USD|15.839
  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   
  7   class MyGUI:
  8   
  9     def __init__( self, filename):
 10       self.builder = gtk.Builder()
 11       self.builder.add_from_file("infiles/treeview3-2.ui") 
 12       self.builder.connect_signals(self)
 13       self.window = self.builder.get_object("window1")
 14       self.load_data(filename)
 15       self.window.show()
 16   
 17   
 18     def load_data(self, filename):
 19       important_currencies = ['EUR','USD','JPY','CAD']
 20       # model creation
 21       model = self.builder.get_object("my_liststore")
 22       f = file(filename, "r")
 23       header = f.readline().strip().split("|") # header in first line
 24       for line in f:
 25         data = line.strip().split("|")
 26         data[2] = int( data[2])
 27         data[4] = float( data[4])
 28         data.append("#FFFF%02X" % int(255*data[4]/40.0/data[2])) # color 
 29         data.append(data[3] in important_currencies) # underline 
 30         model.append( data)
 31       f.close()
 32       # the treeview
 33       treeview = self.builder.get_object("my_treeview")
 34       treeview.connect( "row-activated", self.row_activated)
 35       treeview.set_size_request( 460, 400)
 36       # special rendering function for column 1
 37       col = self.builder.get_object("country_column")
 38       cell = self.builder.get_object("country_icon_cellrenderer")
 39       col.set_cell_data_func(cell, self._render_country)
 40   
 41     def _render_country(self, column, cell, model, iter):
 42       data = model.get_value(iter, 6) # is important
 43       if data:
 44         pixbuf = self.window.render_icon( stock_id=gtk.STOCK_YES, size=gtk.ICON_SIZE_MENU)
 45       else:
 46         pixbuf = None
 47       cell.set_property('pixbuf', pixbuf) # set the property
 48   
 49     def row_activated( self, tree, path, column):
 50       model = tree.get_model()
 51       iter = model.get_iter( path)
 52       country = model.get_value( iter, 0)
 53       d = gtk.MessageDialog( parent=self.window,
 54                              type=gtk.MESSAGE_INFO,
 55                              flags=gtk.DIALOG_DESTROY_WITH_PARENT,
 56                              buttons=gtk.BUTTONS_CLOSE,
 57                              message_format="You selected currency of %s." % country
 58                              )
 59       result = d.run() 
 60       d.destroy()
 61   
 62     def main( self):
 63       gtk.main()
 64   
 65     def on_window1_destroy( self, w):
 66       gtk.main_quit()
 67   
 68   
 69   if __name__ == "__main__":
 70     m = MyGUI("exchange.txt")
 71     m.main()
Screenshot:
Program screenshot gtk-treeview3-2.png
Doba běhu: 1545.3 ms