如何更改Treeview的背景颜色

use*_*746 6 python tkinter python-3.x

我在这里问你如何改变树视图的背景,我试过了

ttk.Style().configure("Treeview", background="#383838")
Run Code Online (Sandbox Code Playgroud)

它完全适用于单元格,但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()
Run Code Online (Sandbox Code Playgroud)

msw*_*msw 3

缺少的选项是我在示例中fieldbackground偶然发现的。所以如果你将它添加到样式声明中

ttk.Style().configure("Treeview", background="#383838", 
 foreground="white", fieldbackground="red")
Run Code Online (Sandbox Code Playgroud)

它按你想要的方式工作。我曾经red让改变变得非常明显;显然你会想要改变它以获得更好的色彩和谐。

  • `tk` 是唯一推出时的游戏,它的设计原则启发了 Qt、GTK 等。如果您刚刚开始一个没有 tkinter 遗留附件的项目,我建议使用 Qt(尽管它使这么说我有点难过)。如果这是用于商业用途,请注意 Tkinter 和 Qt 具有不同的许可证(分别为 BSD 和 LGPL);我没有资格解释这些差异。 (2认同)