Set cellrenderertext foreground color when a row is highlighted

僤鯓⒐⒋嵵緔 提交于 2019-12-13 14:27:23

问题


When I have a gtk.CellRendererText, I can associate its foreground color with one of the tree store's columns, and set the foreground-set attribute to True, to change the color of the text in that column. However, when the row with the colored column is selected, its color disappears, and is the same as any selected cell's color. How do I change the color when it's selected?


回答1:


I've had the same problem and, after trying different alternatives, using the markup property instead of the text property solved the problem. Please find below and example that works in Ubuntu Maverick:

#!/usr/bin/python                               
import gtk


class Application(object):
    def __init__(self):
        window = gtk.Window()

        model = gtk.TreeStore(str)
        model.append(None, row=('Normal row',))
        model.append(None, row=('<span foreground="red">Red row</span>',))

        treeview = gtk.TreeView(model)
        renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn('Column', renderer, markup=0)
        treeview.append_column(column)

        scrolled_window = gtk.ScrolledWindow()
        scrolled_window.add(treeview)

        window.add(scrolled_window)
        window.connect('destroy', lambda w: gtk.main_quit())

        window.show_all()


    def run(self):
        gtk.main()


if __name__ == '__main__':
    Application().run()

In a more complex treeview with the multiple columns that I'm working on, the markup property doesn't seem to work when the row isn't selected. Anyway, usage of both markup and foreground properties at the same time seems to work fine.



来源:https://stackoverflow.com/questions/3629386/set-cellrenderertext-foreground-color-when-a-row-is-highlighted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!