Pygtk:如何更改行的树存储颜色?

时间:2015-01-27 09:55:49

标签: pygtk

目前我正在使用Python和Gtk +库开发一个工具。我在这里遇到了一个问题作为$ subject。是否有任何有效的方法可以将树库" 1/8"的背景​​颜色更改为红色?如下图所示:

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

我使用treestore.set_value来更改它,但不幸的是它为我工作,它只能更改treestore的值。

不知道,所以我在这里提出一个问题。 任何人都可以关注它吗?任何建议,将不胜感激。

1 个答案:

答案 0 :(得分:0)

你可以使用pygtk2.0关于TreeViewColumn.set_cell_data_func方法的文档,阅读关于CellRenderers的重要页面和PyGTK2.0的特殊属性,它们对PyGTK的第3版也有用:)

以下代码可以帮助您:

#!/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()

ageCellDataFunc方法未运行,因为如果设置为set_cell_data_func,则add_attribute方法未运行。