[pygtk] TreeView - Making whole row colored

Neil Dugan neil_dugan at kooee.com.au
Thu Jan 31 12:29:17 WST 2008


Stephen George wrote:
> Hi,
> =

> I am trying to get a treeview (with liststore model) to display rows in =

> different colors based on content in the list.
> =

>  From what I've read in GTK+ 2.0 Tree View Tutorial - Tim-Philipp Muller =

> ( c code based)
> =

> "The most suitable approach for most cases is that you add two columns =

> to your model, one for
> the property itself (e.g. a column COL_ROW_COLOR of type G_TYPE_STRING), =

> and one for the boolean flag of
> the property (e.g. a column COL_ROW_COLOR_SET of type G_TYPE_BOOLEAN). =

> You would then connect these
> columns with the "foreground" and "foreground-set" properties of each =

> renderer. Now, whenever you set
> a row=92s COL_ROW_COLOR field to a colour, and set that row=92s =

> COL_ROW_COLOR_SET field to TRUE, then
> this column will be rendered in the colour of your choice. If you only =

> want either the default text colour or one
> special other colour, you could even achieve the same thing with just =

> one extra model column: in this case you
> could just set all renderer=92s "foreground" property to whatever special =

> color you want, and only connect the
> COL_ROW_COLOR_SET column to all renderer=92s "foreground-set" property =

> using attributes."
> =

> Which I've implemented as attached.
> =

> The setting of foreground seems to be working (half list blue, half red)
> However my implementation seems to be ignoring the foreground-set flag.
> =

> I am expecting to ONLY see my 'special' foreground color when the =

> modified flag is also set to true, and grey/black writing when the =

> foreground-set flag is False.
> =

> I can get grey writing by setting the foreground to None, but I don't =

> belive this was the intent of the above description.
> =

> Am I mis-understanding how this functionality should work, .. or made =

> some errors in my code?
> =

> Thanks for any suggestions.
> Steve
> =


Hi Steve,

I can't see the pattern in why the colors are how they are.  I think =

some of the trouble has to be with some 'foreground-color' set as None.

It would probably be better to not use the 'Modified' column to affect =

the color, and just put the color you want the row in the =

'foreground-color' column (i.e. red,black or blue) and change the =

set-up to.

column.add_attribute(mycell, 'foreground', COLUMN_FOREGROUND)
mycell.set_property('foreground-set', True)




If you must stay with this set-up, this function seems to do what you want

     def _cell_data_func(self, column, cell, model, iter):
		(modified,foreground) =3D =

model.get(iter,COLUMN_MODIFIED,COLUMN_FOREGROUND)
		if foreground =3D=3D None : foreground =3D 'black'
		if modified :
			cell.set_property('foreground',foreground)
		else :
			cell.set_property('foreground','black')
		cell.set_property('foreground-set',True)
		return False

And use "column.set_cell_data_func(mycell, self._cell_data_func)" to =

set it up.

Regards Neil.


-------------- next part --------------
77a78,84
>     def _cell_data_func(self, column, cell, model, iter):
> 		#print "totalcelldatamethod()"
> 		(modified,foreground) =3D model.get(iter,COLUMN_MODIFIED,COLUMN_FOREGRO=
UND)
> 		cell.set_property('foreground',foreground)
> 		cell.set_property('foreground-set',modified)
> 		return False
> =

92,93c99,101
<         column.add_attribute(mycell, 'foreground', COLUMN_FOREGROUND)
<         column.add_attribute(mycell, 'foreground-set', COLUMN_MODIFIED)
---
> #        column.add_attribute(mycell, 'foreground', COLUMN_FOREGROUND)
> #        column.add_attribute(mycell, 'foreground-set', COLUMN_MODIFIED)
>         column.set_cell_data_func(mycell, self._cell_data_func)


More information about the pygtk mailing list