Python Gtk.CellRendererText() 颜色

Python Gtk.CellRendererText() color

此树视图有 5 列,其中包含 USB 设备详细信息列表,它显示设备名称、型号、序列号和状态,在列表中都显示正常,我想要实现的是颜色代码 "status" 仅列,如果 "status" 是读写,则颜色编码为红色,否则为绿色。下面的代码目前将整个列的颜色编码为红色,它只是忽略了任何只读设备。我花了几个小时在网上找到答案,但找不到任何 .谁能帮我解决这个问题?

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)

对于查看此问题的任何人,这里是工作代码:

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)

基本上您需要一个隐藏的列表存储列来保存带有您的颜色的字符串值。然后树视图列需要将前景属性设置为该隐藏列。