我想更改树 View 标题的背景颜色。我已经确定负责此操作的Treeview.Heading布局的element选项: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中工作。代码也不是我的,我找到了它并用它进行了测试。

最佳答案

您处在正确的轨道上,但需要更改border元素而不是cell元素。在Windows上工作时,正在使用Visual Styles API的theme element提供的系统显示树状 View 单元格。在这种情况下,它是HP_HEADERITEM类的HEADER部分。由于这是由系统主题引擎绘制的,因此除了根据状态选择替代外观之外,您无需从Tk对其进行自定义。

如果必须自定义标题的外观,则必须用Tk可以自定义的主题部分替换主题部分,并且default主题是一个不错的选择。我还建议您将其定义为自定义样式,以便您可以重新设置特定小部件的样式,而不必重新设置所有样式。

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')])

我们正在做的是使用与标准树 View 样式相同的布局来定义新的窗口小部件样式,并替换border元素。尽管我们尚未定义其他自定义元素,但是会按层次结构查找这些元素,因此在没有Custom.Treeheading.text的情况下,它将使用Treeheading.text
要使用此功能,我们设置treeview小部件的样式:
t=ttk.Treeview(_frame, style="Custom.Treeview")

最终在Windows 10上看起来像这样:

python-3.x - Tkinter Treeview标题样式-LMLPHP

关于python-3.x - Tkinter Treeview标题样式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42708050/

10-13 07:23
查看更多