如何正确设置Treeview行的前景色和背景色

时间:2019-08-19 16:38:23

标签: python tkinter

我在设置ttk.Treeview的前景色和背景色时遇到问题。

我尝试使用tag_configure,但这似乎也不起作用。我有一些模型代码(如下)用于解决这个问题。 可以更改标题颜色,但不能更改行,不确定我在做什么错。

from tkinter import *
from tkinter.ttk import Treeview, Style


class App(Frame):

    def __init__(self, parent):
        super().__init__()
        self.container = Frame.__init__(self, parent)

        self.tree()

    def tree(self):
        style = Style()

        tv = Treeview(self.container)
        tv.grid(sticky='NSEW')

        tv.insert('', '0', 'item1', text='Item 1', tags='row')
        tv.insert('', '1', 'item2', text='Item 2', tags='row')
        tv.insert('', '2', 'item3', text='Item 3', tags='row')

        tv.insert('item1', '0', 'python1', text='Python Treeview1')
        tv.insert('item1', '1', 'python2', text='Python Treeview2')

        tv.insert('python1', '0', 'thon1', text='Treeview1')
        tv.insert('python1', '1', 'thon2', text='Treeview2')

        tv.heading(f'#{0}',  text='Title')
        style.configure(
            "Treeview.Heading",
            padding=5,
            borderwidth=0,
        )

        style.configure(
            "Treeview",
            foreground='red',
            background="black",
            fieldbackground='blue'
        )
        tv.tag_configure('row', foreground='red')


def main():
    root = Tk()

    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)
    App(root)

    root.mainloop()


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

https://stackoverflow.com/users/7414759/stovfl的注释中指出,Tcl / tk库中存在一个错误。