Changing the selected item colour in a GtkTreeview using python

后端 未结 5 1527
别那么骄傲
别那么骄傲 2021-01-18 07:57

I have a dialog which contains a pygtk.treeview for listing tasks by priority. Each row has the background colour set based on that priority, so for example the highest prio

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-18 08:33

    You can use the method described here – I have tested it briefly and it does the job without flickering. Basically, the trick is to use the "Markup" property of the cell renderer. There's one catch, though: if you want to change the background colour with this method, only the background behind the "actual" text is changed, not the whole row. However, if you want to change the text colour (with

    I have the following CellDataFunc (this is C#, but I hope it's still useful):

    private void CellDataFunc(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter) {
        Item item = (Item) model.GetValue (iter, 0);
        if(cell is CellRendererText) {
            int id = (int)column.GetData("colId");
            string text = "";
            switch(id) {
                case 0: text = item.Name;  break;
                case 1: text = item.Size; break;
                case 2: text = item.Time.ToString();  break;                
            }
            //(cell as Gtk.CellRendererText).Text = text;
            if(item.Highlight) {
                (cell as Gtk.CellRendererText).Markup = 
                                ""+text+"";
            } else {
                (cell as Gtk.CellRendererText).Markup = text;
            }
        }
    }
    

提交回复
热议问题