我试图改变一个树视图小部件标题的背景颜色。但是,对于树视图小部件,它会发生变化。我希望对"树"有不同的背景。标题,而没有任何颜色的"Tree1"我做错了什么?下面是我的代码:
环境:Windows 10 19043.1165 Python 3.9.6
import tkinter as objTK
from tkinter import ttk as objTTK
# Create window
objWindow = objTK.Tk()
# Initialise variables
arrHdr = ["Hdr1", "Hdr2"]
arrData = ["Test1", "Test2"]
# ------------ Treeview 1
fFrame1 = objTK.LabelFrame(objWindow, text="Tree 1")
treeView1 = objTTK.Treeview(columns=arrHdr, show="headings")
treeView1.grid(column=0, row=0, sticky="nsew", in_=fFrame1)
fFrame1.grid_columnconfigure(0, weight=1)
fFrame1.grid_rowconfigure(0, weight=1)
treeView1.heading(0, text="Hd1")
treeView1.heading(1, text="Hd2")
treeView1.insert("", "end", values=arrData)
fFrame1.pack()
# ------------ Treeview 2
fFrame2 = objTK.LabelFrame(objWindow, text="Tree 2")
treeView2 = objTTK.Treeview(columns=arrHdr, show="headings")
objStyle = objTTK.Style(treeView2)
objStyle.theme_use("clam")
objStyle.configure("Treeview.Heading", background="#c3c3c3")
treeView2.grid(column=0, row=0, sticky="nsew", in_=fFrame2)
fFrame2.grid_columnconfigure(0, weight=1)
fFrame2.grid_rowconfigure(0, weight=1)
treeView2.heading(0, text="Hd1")
treeView2.heading(1, text="Hd2")
treeView2.insert("", "end", values=arrData)
fFrame2.pack()
objWindow.bind("<Escape>", lambda wExit: objWindow.destroy())
objWindow.mainloop()
你需要在样式名"Treeview.Heading"创建自定义样式,否则适用于所有Treeview
:
objStyle.configure("my.Treeview.Heading", background="#c3c3c3")
然后将此自定义样式应用于treeView2
:
treeView2 = objTTK.Treeview(columns=arrHdr, show="headings", style="my.Treeview")
修改代码。将此插件作为答案,因为它不允许在注释中添加代码。
import tkinter as objTK
from tkinter import ttk as objTTK
# Create window
objWindow = objTK.Tk()
# Initialise variables
arrHdr = ["Hdr1", "Hdr2"]
arrData = ["Test1", "Test2"]
# ------------ Treeview 1
fFrame1 = objTK.LabelFrame(objWindow, text="Tree 1")
treeView1 = objTTK.Treeview(columns=arrHdr, show="headings")
treeView1.grid(column=0, row=0, sticky="nsew", in_=fFrame1)
fFrame1.grid_columnconfigure(0, weight=1)
fFrame1.grid_rowconfigure(0, weight=1)
treeView1.heading(0, text="Hd1")
treeView1.heading(1, text="Hd2")
treeView1.insert("", "end", values=arrData)
fFrame1.pack()
# ------------ Treeview 2
fFrame2 = objTK.LabelFrame(objWindow, text="Tree 2")
objStyle = objTTK.Style()
#objStyle.theme_use("clam")
objStyle.configure("my.Treeview.Heading", background="#c3c3c3")
treeView2 = objTTK.Treeview(columns=arrHdr, show="headings", style="my.Treeview")
treeView2.grid(column=0, row=0, sticky="nsew", in_=fFrame2)
fFrame2.grid_columnconfigure(0, weight=1)
fFrame2.grid_rowconfigure(0, weight=1)
treeView2.heading(0, text="Hd1")
treeView2.heading(1, text="Hd2")
treeView2.insert("", "end", values=arrData)
fFrame2.pack()
objWindow.bind("<Escape>", lambda wExit: objWindow.destroy())
objWindow.mainloop()
下面是修改后的代码。基本上,主题是"叫嚷"。向标题行添加灰色。所以我必须加上"white"为第一个树视图添加颜色,如下面的代码所示。
import tkinter as objTK
from tkinter import ttk as objTTK
# Create window
objWindow = objTK.Tk()
# Initialise variables
arrHdr = ["Hdr1", "Hdr2"]
arrData = ["Test1", "Test2"]
# ------------ Treeview 1
objStyle = objTTK.Style()
objStyle.theme_use("clam")
objStyle.configure("treeView1.Treeview.Heading", background="white")
fFrame1 = objTK.LabelFrame(objWindow, text="Tree 1")
treeView1 = objTTK.Treeview(columns=arrHdr, show="headings", style="treeView1.Treeview")
treeView1.grid(column=0, row=0, sticky="nsew", in_=fFrame1)
fFrame1.grid_columnconfigure(0, weight=1)
fFrame1.grid_rowconfigure(0, weight=1)
treeView1.heading(0, text="Hd1")
treeView1.heading(1, text="Hd2")
treeView1.insert("", "end", values=arrData)
fFrame1.pack()
# ------------ Treeview 2
fFrame2 = objTK.LabelFrame(objWindow, text="Tree 2")
#objStyle.configure("treeView2.Treeview.Heading", background="#c3c3c3")
treeView2 = objTTK.Treeview(columns=arrHdr, show="headings")
treeView2.grid(column=0, row=0, sticky="nsew", in_=fFrame2)
fFrame2.grid_columnconfigure(0, weight=1)
fFrame2.grid_rowconfigure(0, weight=1)
treeView2.heading(0, text="Hd1")
treeView2.heading(1, text="Hd2")
treeView2.insert("", "end", values=arrData)
fFrame2.pack()
objWindow.bind("<Escape>", lambda wExit: objWindow.destroy())
objWindow.mainloop()