如何完全更改 tkinter.ttk Treeview 上的背景颜色 [英] How to fully change the background color on a tkinter.ttk Treeview

查看:127
本文介绍了如何完全更改 tkinter.ttk Treeview 上的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为我最近使用 tkinter 在 python 3.4.4 中开发的项目制作目录浏览器.我不希望背景成为默认颜色,所以我已经改变了大多数小部件的背景.在我到达 Treeview 之前,我没有遇到任何麻烦.我对 ttk.Style() 不太好,但我仍然设法获得

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.

我检查了是否是调整大小的问题,但一切似乎都井井有条.我还在网上查找了类似的问题,认为我做错了,发现两个链接指向 Bryan Oakley 在 2007 年遇到了同样的问题.

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 回复:如何获得ttk::treeview 没有边框

如何创建没有边框的 ttk::treeview?

本质上,结论是这是在 Windows 上运行时的错误.因此,我有 3 个一般性问题:

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

我是否完全遗漏了一些明显的东西并误解了这些帖子(在这里交叉手指),如果是这样,我做错了什么?或者,如果这真的是一个错误,自 2007 年以来是否有任何解决方案?最后,如果没有任何解决方案,无论解决方案多么笨拙,有没有人找到解决该问题的方法?

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?

推荐答案

为了使 Treeview 的背景全黑,backgroundfieldbackground 选项Treeview 样式需要设置为黑色.

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.

此外,并非所有 ttk 主题都支持 fieldbackground 选项,例如xpnative"和vista"主题.

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

代码:

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

这篇关于如何完全更改 tkinter.ttk Treeview 上的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆