Search code examples
pythontreeviewttk

I want my ttk.treeview to have alternating colours


I want my ttl.treeview table to have an alternating pattern of colours and I tried to add tags and make the background of each odd row distinct by using the tag_configure method, but I haven't been able.

Every row is a row taken from a db table that I want to show.

I also tried to do so in a little and separate program, but it hasn't been effective either.

def show(self, row, column, rowspan):
    self.row = row
    self.column = column
    self.rowspan = rowspan
    global FondoBase


    self.Tabla = ttk.Treeview(FondoBase, columns=self.Columnas[1:], height=40)

    self.Tabla.heading("#0", text=self.Columnas[0])
    for i in self.Columnas[1:]:
        self.Tabla.heading("{}".format(i), text="{}".format(i))

    self.Tabla.grid(row=row, column=column, rowspan=rowspan, sticky="e", pady=30)
    scrol = Scrollbar(FondoBase, command=self.Tabla.yview)

    self.Tabla.config(yscrollcommand=scrol.set)


    scrol.grid(row=row, column=column+1, sticky="wns", rowspan=rowspan, pady=30)



    Conexion = connect('{}'.format(Directorio))

    with Conexion:

        Seleccion = Conexion.cursor()

        Seleccion.execute("""SELECT * FROM {}""".format(self.nombre))

        resultado = Seleccion.fetchall()


    for i in range(len(resultado)):
        if i%2 == 0:
        self.Tabla.insert("", i, text="{}".format(resultado[i][0]), values=(resultado[i][1:]), tags=("par"))
            self.Tabla.tag_configure("par", background="#00ffff")

        else:
            self.Tabla.insert("", i, text="{}".format(resultado[i][0]), values=(resultado[i][1:]), tags=("impar"))

I don't really know what's going on.


Solution

  • -what version of python are you using (python -V) in cmd

    -the last version(3.7) of python seems like it has bugs to color tags

    -if your using the last version just install python 3.6

    -one of your tags doesn't have a color

    self.Tabla.tag_configure("impar", background="#ff008c")