简体   繁体   English

tkinter ttk treeview 彩色行

[英]tkinter ttk treeview colored rows

I am trying to set colors to rows in a tkinter treeview object, using tags and tag_configure.我正在尝试将 colors 设置为 tkinter treeview ZA8CFDE6331_BD59EB2AC96F 中的行,使用tags8911C4B666Z

There has been an earlier discussion on coloring rows which is rather old and seems to work no longer for Python3:之前有一个关于着色行的讨论,它相当古老,似乎不再适用于 Python3:

ttk treeview: alternate row colors ttk treeview:交替行 colors

I have added a brief example.我添加了一个简短的示例。 For me, all rows stay white, independent of whether I execute tag_configure prior or after the insert command.对我来说,所有行都保持白色,与我在插入命令之前还是之后执行 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()

What has changed or what am I missing?发生了什么变化或我错过了什么?

EDIT: Seems that this is a new known bug with a workaround, but I don't get this working: https://core.tcl-lang.org/tk/tktview?name=509cafafae编辑:似乎这是一个新的已知错误,有解决方法,但我没有得到这个工作: https://core.tcl-lang.org/tk/tktview?name=509cafae

EDIT2: I am now using tk Version 8.6.10 (Build hfa6e2cd_0, Channel conda-forge) and python 3.7.3. EDIT2:我现在使用 tk 版本 8.6.10(构建 hfa6e2cd_0,通道 conda-forge)和 python 3.7.3。 Can anyone reproduce this error with this version of python and tk?任何人都可以使用此版本的 python 和 tk 重现此错误吗?

You no longer need to use fixed_map the bug was fixed in tkinter version 8.6.您不再需要使用 fixed_map 该错误已在 tkinter 版本 8.6 中修复。 The following code works fine for me using tkinter 8.6 and python 3.8.2 running in Linux.以下代码适用于我使用在 Linux 中运行的 tkinter 8.6 和 python 3.8.2。

import tkinter as tk
import tkinter.ttk as ttk

def fixed_map(option):
    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('odd', background='green')
lb.tag_configure('even', background='lightgreen')

lb.column("number", anchor="center", width=10)
lb.insert('', tk.END, values = ["1","testtext1"], tags=('odd',))
lb.insert('', tk.END, values = ["2","testtext2"], tags=('even',))
lb.insert('', tk.END, values = ["3","testtext3"], tags=('odd',))
lb.insert('', tk.END, values = ["4","testtext4"], tags=('even',))

lb.pack()

root.mainloop()

That answer of Chuck666 did the trick: https://stackoverflow.com/a/60949800/4352930 Chuck666 的回答成功了: https://stackoverflow.com/a/60949800/4352930

This code works此代码有效

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()

I hope that Chuck666 copies his answer here since I think he has earned the bonus if he shows up.我希望 Chuck666 在这里复制他的答案,因为我认为如果他出现,他已经获得了奖金。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM