【问题标题】:Can I change the foreground color of a single column in Python Treeview?我可以更改 Python Treeview 中单个列的前景色吗?
【发布时间】:2019-12-29 07:00:16
【问题描述】:

这是我在 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')

【问题讨论】:

    标签: python tkinter treeview


    【解决方案1】:

    您可以通过标记您的树视图项目来做到这一点,并使用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()
    

    【讨论】:

      【解决方案2】:

      注意: Henry Yik 的答案在 python 3.7.3 和 3.8.0 中不起作用。但是,它适用于 python 3.6.2。

      您可以使用答案中的代码找到行为差异: How to fully change the background color on a tkinter.ttk Treeview

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-20
        • 2016-07-19
        • 2013-04-14
        • 1970-01-01
        • 1970-01-01
        • 2016-05-08
        • 2018-10-22
        相关资源
        最近更新 更多