1

I have created a pygtk TreeView and wanted to add different colors between each line. I went here and it says that there exists a TreeView Style property that does exactly the same.The property is called 'odd-row-color' and 'even-row-color'. So went to my code and tried to apply this by using the set_property(). But i get an error message for doing that

    self.customer_view.set_property('even-row-color', gtk.gdk.Color(211, 211, 211))
TypeError: object of type `GtkTreeView' does not have property `even-row-color' 

How can achieve that. And where is that property handled?

Apostolos
  • 7,763
  • 17
  • 80
  • 150

1 Answers1

2

You can use css (GTK3) to change the colors, something like:

style_provider = Gtk.CssProvider()
css = '''
GtkTreeView row:nth-child(even) { background-color: shade(@base_color, 0.9); }
GtkTreeView row:nth-child(odd) { background-color: shade(@base_color, 1.0); }
'''
style_provider.load_from_data(css.encode('utf8'))

Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
                            style_provider,
                            Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

Make sure to tell GTK that you want to paint alternate colors:

treeview.set_rules_hint(True)
Daniel
  • 4,525
  • 3
  • 38
  • 52
gianmt
  • 1,162
  • 8
  • 15
  • http://stackoverflow.com/questions/9036812/how-do-you-change-alternating-background-row-colors-of-a-gtk-treeview-in-pygtk i actually used this :) – Apostolos Oct 19 '13 at 18:29
  • 1
    I was trying to convince you to go with GTK3... pygtk is not maintained anymore, you should not use it for new code. – gianmt Oct 19 '13 at 19:37
  • *1*) Is there a none-CSS solution? *2*) How can I differentiate between multiple TreeViews? – buhtz May 10 '18 at 20:03