Issue 36468: Treeview: wrong color change (original) (raw)

Hello, color change with Treeview does not work in Python 3.7.3, but it works with Python 3.7.2.

Test code:

-- coding: utf-8 --

import tkinter from tkinter import ttk root = tkinter.Tk () style = ttk.Style (root) style.configure ("Treeview", foreground="yellow", background="grey", fieldbackground="green") tree = ttk.Treeview (root, columns=('Data')) tree.heading ('#0', text='Item') tree.heading ('#1', text='Data') tree.insert ("", "end", text="Item_0", values=100, tags="A") tree.insert ("", "end", text="Item_1", values=200, tags="B") tree.insert ("", "end", text="Item_2", values=300, tags="C") tree.tag_configure ("A", foreground="black") #Py 3.7.3: no effect tree.tag_configure ("B", foreground="red") tree.pack () root.mainloop ()

Thanks for the report. The difference in behavior appears to be due to a change in Tk. Using the python.org 3.7.2 or .3 installers which use Tk 8.6.8, the colors are displayed. But if I use a Python linked with Tk 8.6.9, the bars are not colored. You can run the following commands in Python to verify which version of Tk is in use:

import tkinter root = tkinter.Tk() root.tk.call('info', 'patchlevel')

In any case, there isn't likely anything Python can do about it as tkinter is pretty much a thin wrapper around calls to Tk and it's difficult to know what the expected Tk behavior is. If you want to pursue this issue, I suggest bringing it up on a Tk forum or checking whether there is a Tk issue about it.

But I had luck! Doing a quick search, I found this Tk ticket which seems to describe your problem and does confirm that the behavior change was introduced in 8.6.9:

https://core.tcl.tk/tk/info/509cafafae

I've just come across the same problem.

For future reference, adding the following code before using a Treeview widget will fix the problem:

def fixed_map(option): # Fix for setting text colour 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 style.map('Treeview', query_opt=option) if
  elm[:2] != ('!disabled', '!selected')]

style = ttk.Style() style.map('Treeview', foreground=fixed_map('foreground'), background=fixed_map('background'))