5
votes

I have just switched from python 3.6 to python 3.7. I have a function which inserts rows in a Treeview tree with tags. The tags are used for giving a foreground color and a background color to the rows inserted to the tree. My code was working ok when I was using python 3.6. Once I switched to 3.7 the rows inserted were not given a background or foreground color but where only given a white background and a black foreground color.

There doesn't seem to be a syntax change in tkinter.ttk from python 3.6 to 3.7 regarding tag configuration or tree insert.

tree.tag_configure('MATCHED', foreground='dark green', background='gray98')
tree.tag_configure('UNMATCHED', foreground='red2', background='gray98')

if match_status== '1':
    tree.insert('', 'end', text=df_row, values=my_value, tag='MATCHED')
elif match_status == '0':
    tree.insert('', 'end', text=df_row, values=my_value, tag='UNMATCHED')`

It is expected that when the rows are inserted to the tree to be given the correct background and foreground color.

Any help is appreciated.

4
Your code worked for me when I used a different background color. As an experiment, what happens when you change background to 'red'? It would also help if you created a working minimal reproducible example. It shouldn't take but a dozen lines or so. That would rule out that some other code is causing the problem.Bryan Oakley

4 Answers

13
votes

Looks like the issue was caused by a newer version of tkinter, not a newer version of Python. This was reported in https://bugs.python.org/issue36468 and https://core.tcl-lang.org/tk/info/509cafafae

Here is a proposed solution. It should be both backward and forward compatible:

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'))
2
votes

I tried a lot of things also with the help of my colleagues. We noticed that the tags only work up to the 3.7.2 Python version (not working on 3.7.3). This was tried in multiple computers and on both operating systems (Windows and Linux).

0
votes

Have you tried changing colors to rgb? Try describing colors in format #RRGGBB where R G and B stands for amount of red green and blue colors ranging from 00 to FF in hexadecimal numbers

0
votes

In my app I use folloiwng approach:

self._my_tree.tag_configure('mygray', background='#DCDCDC')

for router in routers_list:
    self._my_tree.insert('', 'end', router['id'], text=str(router['id']),
                         values=(
                             name,
                             router['location'],
                             router['ip'],
                             router['model'],
                             router['phone_num'],
                             router['provider']
                         )
                         )

    self._my_tree.item(router['id'], tags='mygray')

Try using tags instead of tag