Tkinter Treeview 标题样式
Tkinter Treeview heading styling
我想更改树视图标题的背景颜色。我已经确定了 Treeview.Heading 布局的元素选项对此负责:Treeheading.cell。问题是此设置不适用于 'vista' 主题(我假设是由于绘图问题)。
工作代码(虽然主题看起来很糟糕):
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
print(ttk.Style().theme_names())
ttk.Style().theme_use('default')
ttk.Style().configure("Treeview", background="#383838",foreground="white")
ttk.Style().configure("Treeview.Heading",background = "blue",foreground="Black")
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()
我尝试使用 'vista' 主题:
ttk.Style().element_create("Treeheading.cell","from","default")
ttk.Style().configure("Treeview", background="#383838",foreground="white")
ttk.Style().configure("Treeview.Heading",background = "Blue")
element_create 已解决此问题的其他实例,但使用的是不同的小部件。
谢谢,如有任何帮助,我们将不胜感激。
工作在python 3. 另外代码不是我的,我找到了,用它来测试。
您在正确的轨道上,但需要更改边框元素而不是单元格元素。在您处理 Windows 时,树视图单元格正在使用视觉样式 API 提供的 theme element 系统显示。在本例中,它是 HEADER
class 中的 HP_HEADERITEM
部分。由于这是由系统主题引擎绘制的,除了根据状态选择替代外观外,您无法从 Tk 对其进行自定义。
如果您必须自定义 header 的外观,那么您必须将主题部分替换为 Tk 可以自定义的部分和 default
主题是个不错的选择。我还建议您将其定义为自定义样式,这样您就可以 re-style 特定的小部件,而不必是所有小部件。
style = ttk.Style()
style.element_create("Custom.Treeheading.border", "from", "default")
style.layout("Custom.Treeview.Heading", [
("Custom.Treeheading.cell", {'sticky': 'nswe'}),
("Custom.Treeheading.border", {'sticky':'nswe', 'children': [
("Custom.Treeheading.padding", {'sticky':'nswe', 'children': [
("Custom.Treeheading.image", {'side':'right', 'sticky':''}),
("Custom.Treeheading.text", {'sticky':'we'})
]})
]}),
])
style.configure("Custom.Treeview.Heading",
background="blue", foreground="white", relief="flat")
style.map("Custom.Treeview.Heading",
relief=[('active','groove'),('pressed','sunken')])
我们正在做的是使用与标准树视图样式相同的布局定义新的小部件样式并替换边框元素。虽然我们没有定义其他自定义元素,但它们是按层次结构查找的,因此在没有 Custom.Treeheading.text
的情况下,它将使用 Treeheading.text
。
要使用它,我们设置树视图小部件的样式:
t=ttk.Treeview(_frame, style="Custom.Treeview")
在 Windows 10:
上看起来像这样
我想更改树视图标题的背景颜色。我已经确定了 Treeview.Heading 布局的元素选项对此负责:Treeheading.cell。问题是此设置不适用于 'vista' 主题(我假设是由于绘图问题)。
工作代码(虽然主题看起来很糟糕):
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
print(ttk.Style().theme_names())
ttk.Style().theme_use('default')
ttk.Style().configure("Treeview", background="#383838",foreground="white")
ttk.Style().configure("Treeview.Heading",background = "blue",foreground="Black")
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()
我尝试使用 'vista' 主题:
ttk.Style().element_create("Treeheading.cell","from","default")
ttk.Style().configure("Treeview", background="#383838",foreground="white")
ttk.Style().configure("Treeview.Heading",background = "Blue")
element_create 已解决此问题的其他实例,但使用的是不同的小部件。 谢谢,如有任何帮助,我们将不胜感激。
工作在python 3. 另外代码不是我的,我找到了,用它来测试。
您在正确的轨道上,但需要更改边框元素而不是单元格元素。在您处理 Windows 时,树视图单元格正在使用视觉样式 API 提供的 theme element 系统显示。在本例中,它是 HEADER
class 中的 HP_HEADERITEM
部分。由于这是由系统主题引擎绘制的,除了根据状态选择替代外观外,您无法从 Tk 对其进行自定义。
如果您必须自定义 header 的外观,那么您必须将主题部分替换为 Tk 可以自定义的部分和 default
主题是个不错的选择。我还建议您将其定义为自定义样式,这样您就可以 re-style 特定的小部件,而不必是所有小部件。
style = ttk.Style()
style.element_create("Custom.Treeheading.border", "from", "default")
style.layout("Custom.Treeview.Heading", [
("Custom.Treeheading.cell", {'sticky': 'nswe'}),
("Custom.Treeheading.border", {'sticky':'nswe', 'children': [
("Custom.Treeheading.padding", {'sticky':'nswe', 'children': [
("Custom.Treeheading.image", {'side':'right', 'sticky':''}),
("Custom.Treeheading.text", {'sticky':'we'})
]})
]}),
])
style.configure("Custom.Treeview.Heading",
background="blue", foreground="white", relief="flat")
style.map("Custom.Treeview.Heading",
relief=[('active','groove'),('pressed','sunken')])
我们正在做的是使用与标准树视图样式相同的布局定义新的小部件样式并替换边框元素。虽然我们没有定义其他自定义元素,但它们是按层次结构查找的,因此在没有 Custom.Treeheading.text
的情况下,它将使用 Treeheading.text
。
要使用它,我们设置树视图小部件的样式:
t=ttk.Treeview(_frame, style="Custom.Treeview")
在 Windows 10:
上看起来像这样