插入带有标签的行时,Tkinter Treeview问题 [英] Tkinter Treeview issue when inserting rows with tags
问题描述
我刚刚从python 3.6切换到python 3.7.我有一个函数,可在带有标签的Treeview树中插入行.标签用于为插入到树中的行提供前景色和背景色.当我使用python 3.6时,我的代码工作正常.当我切换到3.7时,插入的行不具有背景色或前景色,而仅具有白色背景和黑色前景色.
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.
在tkinter.ttk中,关于标记配置或树插入,似乎没有从python 3.6到3.7的语法更改.
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.
感谢您的帮助.
推荐答案
该问题似乎是由更新版本的tkinter引起的,而不是更新版本的Python引起的.在 https://bugs.python.org/issue36468 和
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'))
这篇关于插入带有标签的行时,Tkinter Treeview问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!