python-3.xgtk3rendererlistcellrenderercellrenderer

Python Gtk.CellRendererText() color


This treeview has 5 columns with a list of usb devices details , it's showing Device name , Model , Serial number and status , it's all showing fine on the list , what I am trying to achieve is colour code the "status" column only ,if the "status" is READ-WRITE then colour coded as RED otherwise GREEN. The code below is currently colour code entire column as RED ,it's just ignoring any READ-ONLY devices. I've spent a couple of hours finding the answer online but can't find any . Can anyone help me with this ?

def build_device_list(self):
    self.dev_store_len = len(self.device_list_store)
    for i, column_title in enumerate(["Device", "Model", "Serial Number","Size"]):
        self.renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn(column_title, self.renderer, text=i)
        self.device_list_treeview.append_column(column)
        column.set_sort_column_id(i)
    self.renderer_status = gtk.CellRendererText()
    column_status = gtk.TreeViewColumn("Status",self.renderer_status, text=4)
    for x in range(0, self.dev_store_len):
        if self.device_list_store[x][4] == "READ-WRITE":
           self.renderer_status.set_property("foreground", "Red")
           self.device_list_treeview.append_column(column_status)
        else:
           self.renderer_status.set_property("foreground", "Green")
           self.device_list_treeview.append_column(column_status)

    self.scrollable_treelist = gtk.ScrolledWindow()
    self.scrollable_treelist.set_vexpand(True)
    self.outter_box.pack_start(self.scrollable_treelist,False,True,0)
    self.scrollable_treelist.add(self.device_list_treeview)

Solution

  • For anyone looking at this question, here is the working code:

    def build_device_list(self):
        self.dev_store_len = len(self.device_list_store)
        for i, column_title in enumerate(["Device", "Model", "Serial Number","Size"]):
            self.renderer = gtk.CellRendererText()
            column = gtk.TreeViewColumn(column_title, self.renderer, text=i)
            self.device_list_treeview.append_column(column)
            column.set_sort_column_id(i)
        self.renderer_status = gtk.CellRendererText()
        column_status = gtk.TreeViewColumn("Status",self.renderer_status, text=4, foreground=5)
        self.device_list_treeview.append_column(column_status)
        for x in range(0, self.dev_store_len):
            if self.device_list_store[x][4] == "READ-WRITE":
               self.device_list_store[x][5] = "Red"                
            else:
               self.device_list_store[x][5] = "Green" 
        self.scrollable_treelist = gtk.ScrolledWindow()
        self.scrollable_treelist.set_vexpand(True)
        self.outter_box.pack_start(self.scrollable_treelist,False,True,0)
        self.scrollable_treelist.add(self.device_list_treeview)
    

    Basically you need a hidden liststore column that holds string values with your color. Then the treeview column needs the foreground attribute set to that hidden column.