tfv*_*tfv 6 python treeview tkinter ttk
我正在尝试使用标签和 tag_configure 为 tkinter 树视图对象中的行设置颜色。
之前有过关于着色行的讨论,该讨论相当陈旧,似乎不再适用于 Python3:
我添加了一个简短的例子。对我来说,所有行都保持白色,与我是在插入命令之前还是之后执行 tag_configure 无关。
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
w = tk.Label(root, text="Hello, world!")
w.pack()
lb= ttk.Treeview(root, columns=['number', 'text'], show="headings", height =20)
lb.tag_configure('gr', background='green')
lb.column("number", anchor="center", width=10)
lb.insert('',tk.END, values = ["1","testtext1"], tags=('gr',))
lb.insert('',tk.END, values = ["2","testtext2"])
lb.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
发生了什么变化或我错过了什么?
编辑: 似乎这是一个具有解决方法的新已知错误,但我无法正常工作:https : //core.tcl-lang.org/tk/tktview?name=509cafafae
EDIT2: 我现在使用 tk 版本 8.6.10(构建 hfa6e2cd_0,通道 conda-forge)和 python 3.7.3。任何人都可以用这个版本的 python 和 tk 重现这个错误吗?
Chuck666 的回答成功了: https ://stackoverflow.com/a/60949800/4352930
这段代码有效
import tkinter as tk
import tkinter.ttk as ttk
def fixed_map(option):
# 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")]
root = tk.Tk()
style = ttk.Style()
style.map("Treeview",
foreground=fixed_map("foreground"),
background=fixed_map("background"))
w = tk.Label(root, text="Hello, world!")
w.pack()
lb= ttk.Treeview(root, columns=['number', 'text'], show="headings", height =20)
lb.tag_configure('gr', background='green')
lb.column("number", anchor="center", width=10)
lb.insert('',tk.END, values = ["1","testtext1"], tags=('gr',))
lb.insert('',tk.END, values = ["2","testtext2"])
lb.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
我希望 Chuck666 在这里复制他的答案,因为我认为如果他出现,他就已经获得了奖金。
归档时间: |
|
查看次数: |
647 次 |
最近记录: |