簡體   English   中英

我可以更改 Python Treeview 中單個列的前景色嗎?

[英]Can I change the foreground color of a single column in Python Treeview?

這是我在 python tkinter 中的樹視圖的屏幕截圖。我想將“臨界級別”的前景(包括其子級)更改為“紅色”或至少為紅色。

style = Style()
        style.configure("mystyle.Treeview", highlightthickness=0, bd=0, font=('open sans', 10), rowheight=20,
                        foreground='#e8e8e8')
        style.configure("mystyle.Treeview.Heading", font=('open sans', 10, 'bold'), foreground='#000000')

您可以通過標記樹視圖項來實現此目的,並使用tag_configure更改顯示的顏色。

from tkinter import ttk
import tkinter as tk

root = tk.Tk()
tree = ttk.Treeview(root)
tree.pack()
c = tree.insert('', 'end', text='This is critical message', tags=('critical',))
tree.insert(c, 'end', text='This is child of critical message', tags=('critical',))
for i in range(5):
    tree.insert('', 'end', text='This is non-critical message')
tree.tag_configure('critical', background='red',foreground="white")

root.mainloop()

請注意:Henry Yik 的回答在 python 3.7.3 和 3.8.0 中不起作用。 但是,它適用於 python 3.6.2。

您可以使用以下答案中的代碼找到行為差異: How to full change the background color on a tkinter.ttk Treeview

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM