1

Currently I am developing a tool by using Python with Gtk+ library. And I met an issue here as $subject. Is there any effective way for changing the treestore "1/8"'s background color to red ? Something like the below image:

http://imagebin.ca/v/1pZgJ61cWi9D

I use treestore.set_value to change it, but unfortunately it does not work for me, it can only change the value of the treestore.

No idea about it so I raise a question here. Can anybody keep an eye of it ? Any suggestions that would be appreciated.

bluezd
  • 53
  • 10

1 Answers1

1

you can use documents of pygtk2.0 about TreeViewColumn.set_cell_data_func method and read important page about CellRenderers and special properties of PyGTK2.0 that also is useful on version 3 of PyGTK :)

Below code can helping you:

#!/usr/bin/env python

try:
    import pygtk; pygtk.require('2.0')
except:
    pass
import gtk
import pango

COL_FIRST_NAME = 0
COL_LAST_NAME = 1
COL_YEAR_BORN = 2
COL_COLOR = 3

class TreeStoreExample:

    def createAndFillModel(self):

        treestore = gtk.TreeStore(str, str, str, str)
        toplevel1 = treestore.append(None)
        toplevel2 = treestore.append(None)
        child = treestore.append(toplevel2)
        pre_child = treestore.append(child)

        tree = [(toplevel1,
                      COL_FIRST_NAME, "Maria",
                      COL_LAST_NAME, "Incognito",
                      COL_YEAR_BORN, 1982,
                      COL_COLOR, 'black'),

                (toplevel2,
                      COL_FIRST_NAME, "Jane",
                      COL_LAST_NAME, "Average",
                      COL_YEAR_BORN, 1962,
                      COL_COLOR, 'black'),

                (child,
                      COL_FIRST_NAME, "Janinita",
                      COL_LAST_NAME, "Average",
                      COL_YEAR_BORN, 1985,
                      COL_COLOR, 'black'),

                (pre_child,
                      COL_FIRST_NAME, "ABC",
                      COL_LAST_NAME, "DEF",

                      COL_COLOR, 'black')
        ]

        year_now = 2015
        for item in tree:
            iter = item[0]
            treestore.set(*item)
            if item[5] == COL_COLOR:
                treestore.set_value(iter, COL_YEAR_BORN, "age unknown")
                for x in range(1, len(treestore.get_path(iter)) + 1):
                    niter = treestore.get_iter(treestore.get_path(iter)[:x])
                    treestore.set_value(niter, COL_COLOR, "red")
            else:
                treestore.set_value(iter, COL_YEAR_BORN, year_now - item[6])

        return treestore

    def ageCellDataFunc(self, column, renderer, model, iter, data):

        year_now = 2015
        year_born = model.get_value(iter, COL_YEAR_BORN)

        if year_born and year_born <= year_now:
            age = year_now - year_born
            buf = "%u years old" % age
            renderer.set_property("foreground-set", gtk.FALSE)
        else:
            buf = "age unknown"
            #renderer.set_property("foreground", "red")
            model.set_value(iter, COL_COLOR, "red")
            for x in range(1, len(model.get_path(iter))):
                niter = model.get_iter(model.get_path(iter)[:x])
                node = model.set_value(niter, COL_COLOR, "red")
                print model.get_path(iter)[:x], ":", model.get_value(niter, COL_COLOR)

        #renderer.set_property("text", buf)

    def createViewAndModel(self):

        view = gtk.TreeView(self.createAndFillModel())
        self.view = view
        # --- Column 1 ---
        column = gtk.TreeViewColumn()
        column.set_title("First Name")
        view.append_column(column)
        renderer = gtk.CellRendererText()
        column.pack_start(renderer, gtk.TRUE)
        column.add_attribute(renderer, "text", COL_FIRST_NAME)

        # --- Column 2 ---
        column = gtk.TreeViewColumn()
        column.set_title("Last Name")
        view.append_column(column)
        renderer = gtk.CellRendererText()
        column.pack_start(renderer, gtk.TRUE)
        column.add_attribute(renderer, "text", COL_LAST_NAME)
        renderer.set_property("weight", pango.WEIGHT_BOLD)

        # --- Column 3 ---
        renderer = gtk.CellRendererText()
        renderer.set_property('foreground-set',True)
        column = gtk.TreeViewColumn("age", renderer, foreground=COL_COLOR)
        column.pack_start(renderer, gtk.TRUE)
        column.add_attribute(renderer, "text", COL_YEAR_BORN)
        column.add_attribute(renderer, "foreground", COL_COLOR)
        #column.set_title("age")
        view.append_column(column)
        #column.set_cell_data_func(renderer, self.ageCellDataFunc, None)

        #model = self.createAndFillModel()

        #view.set_model(model)

        view.get_selection().set_mode(gtk.SELECTION_NONE)

        return view

    def __init__(self):

        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.connect("delete_event", gtk.mainquit)

        view = self.createViewAndModel()
        window.add(view)

        window.show_all()

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    TreeStoreExample()
    main()

The ageCellDataFunc method not run because if set set_cell_data_func then add_attribute method not running.

M.javid
  • 6,387
  • 3
  • 41
  • 56
  • I have read it before, but unfortunately it did not work for me. eg, in parent treestore I have already set the text and background, in the child treestore I do not think it could change the parent's treestore's background. – bluezd Jan 28 '15 at 02:55
  • http://stackoverflow.com/questions/27745585/show-icon-or-color-in-gtk-treeview-tree/27850438 – M.javid Jan 28 '15 at 05:09
  • It could only modify the current treestore row renderer's attributes. Could not change the parent treestore row's attributes, eg:background. – bluezd Jan 28 '15 at 07:21
  • You must store all path of ancestors of the destination row to use in colorising rows. colorising parents not modifing automatically, but you can use `get_path` method of `ListStore' to get parents of the row. – M.javid Jan 28 '15 at 07:43
  • Thanks for you reply, I use treestore not liststore, you mean use the CellRendererText.set_property to set the parents' attribute ? such as: cell.set_property("foreground", "red"), but cell works on the current row, could not set the parent row. – bluezd Jan 28 '15 at 08:23
  • ListStore and TreeStore both have `get_path` method, can you attach a sample code to the your post? i can change code to get your certain result... – M.javid Jan 28 '15 at 08:35
  • Here is the test code [http://pastebin.com/njuTwKxR](http://pastebin.com/njuTwKxR) "age uknown" is red, I want all of its parents' age column are all color http://imagebin.ca/v/1pgMfyzHDD9f . Could you help implementing it ? Thanks ! – bluezd Jan 28 '15 at 08:49
  • I tried several ways to implement it but always not work, any ideas here ? – bluezd Jan 29 '15 at 06:00