Title: Treeview: wrong color change
Type: behavior Stage: resolved
Components: Tkinter Versions: Python 3.7
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: gpolo, guillaumeb, mrabarnett, ned.deily, serhiy.storchaka
Priority: normal Keywords:

Created on 2019-03-29 08:31 by guillaumeb, last changed 2019-05-16 23:20 by mrabarnett. This issue is now closed.

File name Uploaded Description Edit
test_treeview_color.png guillaumeb, 2019-03-29 08:31 Treeview difference between Python v3.7.2 and v3.7.3
Messages (3)
msg339101 - (view) Author: Burgunder (guillaumeb) Date: 2019-03-29 08:31
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 ()
msg339142 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2019-03-29 20:33
Thanks for the report.  The difference in behavior appears to be due to a change in Tk. Using the 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()'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:
msg342678 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2019-05-16 23:20
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:
    # Returns the style map for 'option' with any styles starting with
    # ('!disabled', '!selected', ...) filtered out.

    # returns an empty list for missing options, so this
    # should be future-safe.
    return [elm for elm in'Treeview', query_opt=option) if
      elm[:2] != ('!disabled', '!selected')]

style = ttk.Style()'Treeview', foreground=fixed_map('foreground'),
Date User Action Args
2019-11-28 06:38:05ned.deilylinkissue38917 superseder
2019-07-10 14:54:21ned.deilylinkissue37546 superseder
2019-05-16 23:20:50mrabarnettsetnosy: + mrabarnett
messages: + msg342678
2019-03-29 20:33:08ned.deilysetstatus: open -> closed

nosy: + ned.deily
messages: + msg339142

resolution: third party
stage: resolved
2019-03-29 10:54:41SilentGhostsetnosy: + gpolo, serhiy.storchaka
2019-03-29 08:31:58guillaumebcreate