Home>

I want to change the background color and text color of the specified line in Treeview.
tkinter ttk treeview colored lines
I was able to set the text color by referring to, but I could not change the background color.
(I want to use a different color when the line is selected when clicking.)

Also, what should I do if I want to change the color of the specified line later?
For exampleddd, eee, fffFrom blue to green.

from tkinter import ttk
import tkinter as tk
def fixed_map (option):
    # Fix for setting text color for Tkinter 8.6.9
    # From: https://core.tcl.tk/tk/info/509cafafae
    #
    # Returns the style map for'option' with any styles starting with
    # ('! disabled','! selected', ...) filtered out.
    # style.map () returns an empty list for missing options, so this
    # should be future-safe.
    return [elm for elm in s.map ('Treeview', query_opt = option) if
        elm [: 2]! = ('! Disabled','! selected')]

if __name__ == "__main__":
    win = tk.Tk ()
    win.geometry ("500x400")
    # Get width
    return value of win.update_idletasks () # winfo_width () 1 Avoidance
    win_width = win.winfo_width ()
    # Stretch
    win.rowconfigure (0, weight = 1)
    win.columnconfigure (0, weight = 1)
    #Style_Theme
    s = ttk.Style ()
    s.theme_use ("clam")
    s.map ('Treeview','Treeview',
        foreground = fixed_map ('foreground'),
        background = fixed_map ('background')
    )

    tree = ttk.Treeview (win)
    #Number of columns
    tree ["columns"] = (1, 2, 3)
    tree ["show"] = "headings"
    #Specify column width
    tree.column (1, width = int (win_width/3))
    tree.column (2, width = int (win_width/3))
    tree.column (3, width = int (win_width/3))
    #Header
    tree.heading (1, text = "Data1")
    tree.heading (2, text = "Data2")
    tree.heading (3, text = "Data3")
    tree.grid (row = 0, column = 0, sticky = "nsew")
    tree.tag_configure ("mytag1", background = "red", foreground = "blue")
    tree.tag_configure ("mytag2", background = "green", foreground = "yellow")
    #Insert
    id = []
    id.append (tree.insert ("", "end", value = ["aaa", "bbb", "ccc"]))
    id.append (tree.insert ("", "end", value = ["ddd", "eee", "fff"], tags = ("mytag1")))
    id.append (tree.insert ("", "end", value = ["ggg", "hhh", "iii"], tags = ("mytag2")))
    # Change tag later
    # tree.item (id [1], tags = "")
    # Get all ids
    #print (tree.get_children ())
    win.mainloop ()
  • Answer # 1

    It is a known bug on the tcl/tk side (ver8.6) and can be dealt with by patch.

      win.tk.eval ("" "
            ttk :: style map Treeview \
            -foreground {disabled SystemGrayText \
                         selected SystemHighlightText} \
            -background {disabled SystemButtonFace \
                         selected System Highlight}
        "" ")

    To change the color of the specified line, tag the specified line,
    You can set the color for the tag.


    Related: PySimpleGUI question, but information about Treeview issues

    stackoverflow: Change the text color only for a specific line in the Table element of PySimpleGUI
    How to deal with patch

    stackoverflow: Pysimplegui cuts off the screen
    * Treeview color change does not work before python 3.7.2 (depending on the version of the built-in tk library)