Re: PyGObject Maybe bug with alternate row colors in a Gt.TreeView




 
I took a look at the stackoverflow discussion about this and there is some sample code there to change colors 
of individual records in the treeview list. You can no longer do even and odd colors in CSS but being able to 
connect a cell renderer property to a column value can be very useful. 

The following gives an error if I use "cell-background" which is stated in the documentation. Maybe just a 
typo someplace but "cell_background" seems to work fine for binding the color column to the cell-background 
property. You can pass some common color names or a rgb hex value for a color string.

Maybe a pretty terrible color combo going here but it might be a useful technique to highlight particular 
records to bring a users attention to them. A lot that you can do with a treeview.

Eric

import gi
gi.require_version('Gtk', '3.0')

from gi.repository import Gtk

class MainWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Stripes")
        self.set_default_size(200, 200)
        self.set_position(Gtk.WindowPosition.CENTER)

        self.liststore = Gtk.ListStore(str, str)
        self.liststore.append(["Apples", "cyan"])
        self.liststore.append(["Oranges", "yellow"])
        self.liststore.append(["Pears", "cyan"])
        self.liststore.append(["Currants", "yellow"])
        self.liststore.append(["Gooseberries", "#00ffff"])
        self.liststore.append(["Strawberries", "#ffff00"])

        self.treeview = Gtk.TreeView(model=self.liststore)
        self.treeview.set_vexpand(True)
        self.treeview.set_hexpand(True)

        self.renderer = Gtk.CellRendererText()
        self.renderer.set_property("font", "Arial 16")
        self.renderer.set_property("xalign", 0.5)

        #Set the column to get the background color from. Need cell_background instead of cell-background???
        self.column = Gtk.TreeViewColumn("Fruit", self.renderer, text = 0, cell_background = 1)
        self.column.set_expand(True)

        self.treeview.append_column(self.column)

        self.add(self.treeview)

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

 




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]