如何在Python 3.7.3 中配置ttk.Treeview 项目颜色?
How to configure ttk.Treeview item color in Python 3.7.3?
我想自定义 ttk.Treeview
中单个项目的样式。示例代码:
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
tree = ttk.Treeview(root)
# Inserted at the root, program chooses id:
tree.insert('', 'end', 'foo', text='Foo', tags=['red_fg'])
# Inserted underneath an existing node:
tree.insert('foo', 'end', text='Bar', tags=['blue_fg'])
# tag's order can be important
tree.tag_configure("red_fg", foreground="red")
tree.tag_configure("blue_fg", foreground="blue")
tree.pack()
root.mainloop()
这在 Python 3.6.8(字体为 red/blue)中运行良好,但在 Python 3.7.3(字体为黑色)中则完全无效。我已经在 32 位和 64 位的 Windows 7 和 10 中对此进行了测试。
我怎样才能让它在新版本中工作?
它工作正常没问题我们 运行 它在 python 3.7x 上。这里附上截图。
enter image description here
也许我来晚了一点但是...
您肯定面临一个影响 Windows Python build shipping tk v8.6.9 的已知错误。
要解决此问题,请在您的代码之上添加此代码(在实例化 Treeview 之前):
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
s = ttk.Style()
#from os import name as OS_Name
if root.getvar('tk_patchLevel')=='8.6.9': #and OS_Name=='nt':
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 s.map('Treeview', query_opt=option) if elm[:2] != ('!disabled', '!selected')]
s.map('Treeview', foreground=fixed_map('foreground'), background=fixed_map('background'))
我想自定义 ttk.Treeview
中单个项目的样式。示例代码:
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
tree = ttk.Treeview(root)
# Inserted at the root, program chooses id:
tree.insert('', 'end', 'foo', text='Foo', tags=['red_fg'])
# Inserted underneath an existing node:
tree.insert('foo', 'end', text='Bar', tags=['blue_fg'])
# tag's order can be important
tree.tag_configure("red_fg", foreground="red")
tree.tag_configure("blue_fg", foreground="blue")
tree.pack()
root.mainloop()
这在 Python 3.6.8(字体为 red/blue)中运行良好,但在 Python 3.7.3(字体为黑色)中则完全无效。我已经在 32 位和 64 位的 Windows 7 和 10 中对此进行了测试。
我怎样才能让它在新版本中工作?
它工作正常没问题我们 运行 它在 python 3.7x 上。这里附上截图。
enter image description here
也许我来晚了一点但是...
您肯定面临一个影响 Windows Python build shipping tk v8.6.9 的已知错误。
要解决此问题,请在您的代码之上添加此代码(在实例化 Treeview 之前):
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
s = ttk.Style()
#from os import name as OS_Name
if root.getvar('tk_patchLevel')=='8.6.9': #and OS_Name=='nt':
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 s.map('Treeview', query_opt=option) if elm[:2] != ('!disabled', '!selected')]
s.map('Treeview', foreground=fixed_map('foreground'), background=fixed_map('background'))