【问题标题】:How to change the background color of a Treeview如何更改 Treeview 的背景颜色
【发布时间】:2013-08-04 16:07:04
【问题描述】:

我是来问你如何改变树形视图的背景,我试过了

ttk.Style().configure("Treeview", background="#383838")

它仅适用于单元格,但 Treeview 的其余部分保持白色。

我试图改变窗口的背景,也改变了框架,但它不起作用。

那么,怎么做,我相信你知道。

提前谢谢你:)

代码

from tkinter import *
from tkinter import ttk

p=Tk()

separator = PanedWindow(p,bd=0,bg="#202322",sashwidth=2)

separator.pack(fill=BOTH, expand=1)

_frame = Frame(p,bg="#383838")

t=ttk.Treeview(_frame)

t["columns"]=("first","second")
t.column("first",anchor="center" )
t.column("second")
t.heading("first",text="first column")
t.heading("second",text="second column")
t.insert("",0,"dir1",text="directory 1")
t.insert("dir1","end","dir 1",text="file 1 1",values=("file 1 A","file 1 B"))
id=t.insert("","end","dir2",text="directory 2")
t.insert("dir2","end",text="dir 2",values=("file 2 A","file 2 B"))
t.insert(id,"end",text="dir 3",values=("val 1 ","val 2"))
t.insert("",0,text="first line",values=("first line 1","first line 2"))
t.tag_configure("ttk",foreground="black")

ysb = ttk.Scrollbar(orient=VERTICAL, command= t.yview)
xsb = ttk.Scrollbar(orient=HORIZONTAL, command= t.xview)
t['yscroll'] = ysb.set
t['xscroll'] = xsb.set

ttk.Style().configure("Treeview", background="#383838",foreground="white")
p.configure(background='black')

t.grid(in_=_frame, row=0, column=0, sticky=NSEW)
ysb.grid(in_=_frame, row=0, column=1, sticky=NS)
xsb.grid(in_=_frame, row=1, column=0, sticky=EW)
_frame.rowconfigure(0, weight=1)
_frame.columnconfigure(0, weight=1)

separator.add(_frame)

w = Text(separator)
separator.add(w)

p.mainloop()

【问题讨论】:

  • 你说“我试图改变窗口的背景,也改变了框架,但它不起作用”。向我们展示代码,以便我们能够为您提供帮助。
  • 是的,抱歉,“样式代码”:_frame = Frame(p,bg="#383838") p.configure(background='black')

标签: python python-3.x tkinter


【解决方案1】:

缺少的选项是fieldbackground,这是我偶然发现的in an example。所以如果你把它添加到样式声明中

ttk.Style().configure("Treeview", background="#383838", 
 foreground="white", fieldbackground="red")

它可以随心所欲地工作。我使用red 使更改非常明显;显然你会想要改变它以获得更好的色彩和谐。

【讨论】:

  • 感谢您的回答,但这不起作用:image.noelshack.com/fichiers/2013/32/1375729684-capture.png 如您所见,它仍然是白色的,此外,无论有无“fieldbackground”,我都看不出有什么区别。很奇怪 :( 为什么这不起作用?我确切地说我想在树视图的开头和水平滚动条之间更改白色背景。
  • 对不起,我帮不了你。我确实在我的安装中用你的代码测试了它,它是红色的。不知道。
  • 那么,问题是由 python 引起的吗?任何人都可以测试这段代码,并告诉我他是否工作?我使用最新版本的 python (3.3.2),我在 Windows 8 上运行。它也不适用于 Windows 7。你的版本是什么?
  • 我在 Linux 上的纯 Python 3.3.1 上进行了测试。我对它在 Windows 下无法运行并不感到非常惊讶,因为 Tkinter 是一个相对较旧的模块,并且可能没有对辅助平台上的晦涩功能进行太多测试。
  • tk 是推出时唯一的游戏,其设计原则启发了 Qt、GTK 等。如果您刚开始一个没有 tkinter 遗留附件的项目,我d 建议 Qt(尽管这样说让我有点难过)。如果这是用于商业用途,请注意 Tkinter 和 Qt 具有不同的许可证(分别为 BSD 和 LGPL);我没有资格解释这些差异。
【解决方案2】:
if flag == False:
    tree.insert('', 'end', values=(valx[0], valx[1], valx[2], valx[3]),tags=('odd',))
else:
    tree.insert('', 'end', values=(valx[0], valx[1], valx[2], valx[3]),tags=('even',))

tree.tag_configure('odd', background='#008001')
tree.tag_configure('even', background='#FFFF00')

【讨论】:

  • 这个答案的上下文是什么?这是解决OP问题的方法吗?
【解决方案3】:

如果@msw 建议的答案对您不起作用,这是一个可能的解决方案。

对于那些即使在配置样式和/或标签后也无法看到树视图着色方面的任何变化的人,请参考此视频:Youtube link

Easy Fix:@Durai找到对此article的评论

添加此行以在您的代码中编辑地图配置:

# set backgound and foreground color when selected
style.map('Treeview', background=[('selected', '#BFBFBF'), foreground=[('selected', 'black')])

另一种硬编码方式:

ttk 的主题文件存在于 C://tcl/tk8.6/ttk/

vistaTheme.tcl 是 ttk 使用的默认主题。

您需要将以下代码更改/添加到 Treeview(文件结尾)下的主题文件中才能正常工作:

ttk::style map Treeview \
        -background {disabled $colors(-frame)\
                        selected lightblue\
                        selected $colors(-selectbg)} \
        -foreground {disabled $colors(-disabledfg)\
                        selected black\
                        selected $colors(-selectfg)}

在我的情况下,我在主题文件中缺少这些行,所以我将它们添加到 Treeview 下方,然后运行我的代码,我终于可以在我的 Treeview 上看到颜色

【讨论】:

    猜你喜欢
    • 2017-10-04
    • 2021-02-10
    • 2016-05-08
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2018-02-13
    • 2012-01-02
    相关资源
    最近更新 更多