9
votes

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. Error

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?

2
Have you tried style.configure("Treeview", fieldbackground="black")?j_4321
I just tried that now and it seems to have no effectzlmonroe
I think it is related to the ttk theme you are using, it should work with the theme "clam" (style.them_use("clam")).j_4321
That was it j_4321. Thanks a bunch! (I'd love to accept your answer if you'd submit it as a full fledged answer outside the comments. Thank you for your help as well Mr. Yahlizlmonroe

2 Answers

12
votes

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()
4
votes

Use to use this code instead :

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

Hopefully this will help you,

Yahli.