问题
Right now I have this code:
tree = ttk.Treeview(root)
style = ttk.Style()
style.configure("Treeview", foreground='#00337f',background="#ffc61e")
style.configure("Treeview.Heading", background="purple",foreground='#00337f',font=(20))
tree["columns"] = ("one", "two", "three")
tree.column("one", width=150)
tree.column("two", width=150)
tree.column("three", width=210)
tree.heading("one", text="Naar")
tree.heading("two", text="Spoor")
tree.heading("three", text="Vetrektijd")
tree.tag_configure('monospace', font=20)
tree['show'] = 'headings'
tree.place(x=0, y=0)
Everything works fine except 3 things:
Whenever I run the program the treeview background is still white but it needs to be yellow from the beginning. Right now it becomes yellow whenever the treeview is filled.
The second problem is that I changed the background of the heading to purple as you can see in the code above but it still is white. So how can I make it purple?
The third problem is there is still a line after the treeview is filled:
回答1:
The first problem can be solved by adding fieldbackground="#ffc61e"
in the configuration of the Treeview
style. The background option corresponds to the background of the items only. However, not all themes takes the fieldbackground
option into account (see the second problem).
The second problem is theme related. The theme you are using does not support color change for the headings, so I suggest you to use another one like 'clam' or 'alt': style.theme_use('clam')
.
To remove the borders of the buttons in the heading, you can set the border width to 0:
style.configure("Treeview.Heading", borderwidth=0)
.
Unfortunately, this does not work for the Treeview border, so the solution is to set all colors of the border to the background color:
style.configure("Treeview", lightcolor="#ffc61e", bordercolor="#ffc61e",
darkcolor="#ffc61e")
来源:https://stackoverflow.com/questions/46957137/tkinter-change-style-questions