And*_* P. 5 tags treeview tkinter insert
我刚刚从 python 3.6 切换到 python 3.7。我有一个函数,可以在带有标签的 Treeview 树中插入行。标签用于为插入到树中的行提供前景色和背景色。当我使用 python 3.6 时,我的代码工作正常。一旦我切换到 3.7,插入的行没有给出背景或前景色,而只有白色背景和黑色前景色。
tkinter.ttk 从 python 3.6 到 3.7 似乎没有关于标签配置或树插入的语法变化。
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')`
Run Code Online (Sandbox Code Playgroud)
预计当行被插入到树中时会被赋予正确的背景和前景色。
任何帮助表示赞赏。
Jak*_*ria 12
看起来这个问题是由较新版本的 tkinter 引起的,而不是由较新版本的 Python 引起的。这是在https://bugs.python.org/issue36468和https://core.tcl-lang.org/tk/info/509cafafae 中报告的
这是一个建议的解决方案。它应该向后和向前兼容:
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'))
Run Code Online (Sandbox Code Playgroud)