How to fully change the background color on a tkinter.ttk Treeview

风格不统一 提交于 2020-02-03 05:49:53

问题


I have been attempting to make a directory browser for a recent project of mine that I'm developing in python 3.4.4 with tkinter. I do not want the background to be the default color, so I have gone about changing the background of most widgets. I didn't have any trouble until I got to the Treeview. I'm not too good with ttk.Style(), but I still managed to get

ttk.Style().configure("Treeview", background="black",
                foreground="white")

to work, however this only changes the background of the area included in the widget.

I checked to see if it was a resizing issue, but everything seems to be in order. I also looked for similar issues online thinking I was doing it wrong and found two links pointing to Bryan Oakley having the same issue back in 2007.

Bryan Oakley Re: how to get a ttk::treeview with no border

how to create a ttk::treeview without a border?

Essentially, the conclusion was that this is a bug when run on windows. Because of this, I'm left with 3 general questions:

Am I just entirely missing something obvious and misunderstanding these posts (crossing fingers here) and if so what am I doing wrong? Or if this was really a bug, has there been any solution to it since 2007? And finally if there has not been any solution, does anyone have a way that they've found to work around the issue, no matter how hacky of a solution?


回答1:


To make the background of a Treeview totally black, both the background and the fieldbackground options of the Treeview style need to be set to black.

In addition, not all ttk themes support the fieldbackground option, like the "xpnative" and "vista" themes.

Code:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

style = ttk.Style(root)
# set ttk theme to "clam" which support the fieldbackground option
style.theme_use("clam")
style.configure("Treeview", background="black", 
                fieldbackground="black", foreground="white")

tree = ttk.Treeview(root)
tree.insert("", 0, "item", text="item")
tree.pack()

root.mainloop()



回答2:


Use to use this code instead :

ttk.Style().configure("Treeview", background="black", 
foreground="white", fieldbackground="black")

Hopefully this will help you,

Yahli.



来源:https://stackoverflow.com/questions/43816930/how-to-fully-change-the-background-color-on-a-tkinter-ttk-treeview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!