此TreeView具有5列,其中包含USB设备详细信息列表,它显示设备名称,模型,序列号和状态,所有这些都显示在列表上,我试图实现的目标是颜色代码"状态"仅列,如果读取"状态",则将颜色编码为红色,否则将其编码为绿色。下面的代码当前是颜色代码整列作为红色,它只是忽略了任何只读设备。我花了几个小时在网上找到答案,但找不到任何答案。谁能帮我吗?
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)
基本上,您需要一个隐藏的ListStore列,该列以您的颜色保存字符串值。然后,TreeView列需要将前景属性设置为那个隐藏的列。