0

I am trying to color my tkinter treeview using tags but it doesn't work even tho I have followed a few tutorials and I think I am doing everything the right way.

self.sidebar_button_event()
            self.ultimo = "Inventario"
            self.cajas_frame = customtkinter.CTkTabview(self, height=250)
            self.cajas_frame.add("Cajas")
            self.cajas_frame.tab("Cajas").grid_columnconfigure(0, weight=1)
            self.cajas_frame.tab("Cajas").grid_rowconfigure(0, weight=1)
            self.cajas_frame.grid(row=0, column=1, padx=(20, 20), pady=(20, 20), sticky="new", columnspan=3)
            self.setTablaCajas()

            n = 0
            for f in self.inventario.datosCajas():
                if n % 2 == 0:
                    self.cajas.insert(parent='', index='end', iid=n, values=f, tags=('par',))
                else:
                    self.cajas.insert(parent='', index='end', iid=n, values=f, tags=('impar',))
                self.cajas.bind("<<TreeviewSelect>>", self.clickCajas)
                n += 1
            n = 0
            
            self.cajas.tag_configure('par', background='orange', )
            self.cajas.tag_configure('impar', background='purple')

This is what I get

Could it be because of me using customtkinter and tkinter?

PS: I already tried using Style.configure and it does change it's appearance, but it doesn't seem to work this way and I want odd and even rows to have a different color.

acw1668
  • 40,144
  • 5
  • 22
  • 34
MakiMH3
  • 1
  • 2
  • This [question](https://stackoverflow.com/questions/61105126/tag-configure-is-not-working-while-using-theme-ttk-treeview) may help. – acw1668 Jan 31 '23 at 08:51

1 Answers1

0

I used a working example to explain the color tag function for the rows:

# Source for base treeview https://www.pythontutorial.net/tkinter/tkinter-treeview/
# extendet with style and color line 11 to 13, 24, 33 to 38 and 60,61
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo

root = tk.Tk()
root.title('Treeview demo')
root.geometry('620x200')

# style the widget
s = ttk.Style()
s.theme_use('clam')

# define columns
columns = ('first_name', 'last_name', 'email')

tree = ttk.Treeview(root, columns=columns, show='headings')

# define headings
tree.heading('first_name', text='First Name')
tree.heading('last_name', text='Last Name')
tree.heading('email', text='Email')
s.configure('Treeview.Heading', background="green3")

# generate sample data
contacts = []
for n in range(1, 100):
    contacts.append((f'first {n}', f'last {n}', f'email{n}@example.com'))

# add data to the treeview AND tag the row color
i = 1
for contact in contacts:
    i += 1
    if i%2:
        tree.insert('', tk.END, values=contact, tags = ('oddrow',))
    else:
        tree.insert('', tk.END, values=contact, tags = ('evenrow',))
    


def item_selected(event):
    for selected_item in tree.selection():
        item = tree.item(selected_item)
        record = item['values']
        # show a message
        showinfo(title='Information', message=','.join(record))


tree.bind('<<TreeviewSelect>>', item_selected)

tree.grid(row=0, column=0, sticky='nsew')

# add a scrollbar
scrollbar = ttk.Scrollbar(root, orient=tk.VERTICAL, command=tree.yview)
tree.configure(yscroll=scrollbar.set)
scrollbar.grid(row=0, column=1, sticky='ns')

# style row colors
tree.tag_configure('oddrow', background='lightgrey')
tree.tag_configure('evenrow', background='white')

# run the app
root.mainloop()

Output: enter image description here

Hermann12
  • 1,709
  • 2
  • 5
  • 14