Change background color of a cell in TreeView


Can you change the background color of a particular cell in a treeview according to the information it contains? In plan if it contains a 0 the background is green, if it contains a 1 the background is orange and if it contains a 2 the background is red.

asked by Alfredo Lopez Rodes 12.11.2018 в 12:53

1 answer


You can do it with tags .

When creating the tree element, you define certain tags associating each of the presentation options (background color, foreground, etc.). So:

 self.tree = ttk.Treeview(self)
 self.tree.tag_configure('cero', background='green')
 self.tree.tag_configure('uno', background='orange')
 self.tree.tag_configure('dos', background='red')

Then, when you insert the elements in that tree you put the appropriate label:

 etiquetas = {0: "cero", 1: "uno", 2: "dos"}
 for dato in ...:
     tag = etiquetas.get(dato)
     self.tree.insert(parent, 'end', text=str(dato), tags=(tag,))
answered by 12.11.2018 / 13:33