问题
I'm having a problem setting the foreground and background colors for ttk.Treeview.
I have tried using tag_configure but that doesn't seem to work either. I have some mockup code (below) that I'm using to figure this out. It's possible to change the header colors but not the rows, not sure what I'm doing wrong.
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:
There is a bug in the Tcl/tk library as noted in the comments from, https://stackoverflow.com/users/7414759/stovfl.
来源:https://stackoverflow.com/questions/57560784/how-correctly-to-set-treeview-row-foreground-and-background-colors