style = ttk.Style(my_w)
style.theme_use("clam") # set theam to clam
style.configure("Treeview", background="black",
fieldbackground="black", foreground="white")
style.configure('Treeview.Heading', background="PowderBlue")
The last line in above code adds different background color to headings. style.configure('Treeview', rowheight=100)
r1 = tk.Radiobutton(my_w, text='Black', variable=r1_v, value='black',
command=lambda:my_upd('black'))
Inside the function my_upd() we will check the parameter value and accordingly update the style.
def my_upd(col):
if col=='white':
style.configure('Treeview',background="white",
fieldbackground="white", foreground="black")
elif col=='yellow':
style.configure('Treeview',background="yellow",
fieldbackground="yellow", foreground="black")
else:
style.configure('Treeview',background="black",
fieldbackground="black", foreground="white")
We can include the font style also.
font1=['Times',12,'normal']
To one of the option we can assign the font style like this.
elif col=='yellow':
style.configure('Treeview',background="yellow",
fieldbackground="yellow", foreground="black",font=font1)
from tkinter import ttk
import tkinter as tk
# Creating tkinter my_w
my_w = tk.Tk()
my_w.geometry("300x250") # width and height of the window
my_w.title("www.plus2net.com")
trv=ttk.Treeview(my_w,selectmode='browse',show='headings',height=5)
trv.grid(row=1,column=1,columnspan=3,padx=30,pady=10)
# column identifiers
trv["columns"] = ("1", "2")
# Defining headings, other option is tree
trv['show'] = 'headings tree'
# width of columns and alignment
trv.column("#0", width = 80, anchor ='c')
trv.column("1", width = 10, anchor ='c')
trv.column("2", width = 100, anchor ='c')
# Headings
# respective columns
trv.heading("#0", text ="Label",anchor='c')
trv.heading("1", text ="id")
trv.heading("2", text ="Name",anchor='c')
trv.insert("",'end',iid=1,text='First',values=(1,'n1-Alex'))
trv.insert("",'end',iid=2,text='second',values=(2,'n2-Ravi'))
trv.insert("",'end',iid=3,text='third',values=(3,'n3-Ronn'))
style = ttk.Style(my_w)
style.theme_use("clam") # set theam to clam
style.configure("Treeview", background="black",
fieldbackground="black", foreground="white")
style.configure('Treeview.Heading', background="PowderBlue")
r1_v = tk.StringVar(value='black') # We used string variable here
def my_upd(col):
if col=='white':
style.configure('Treeview',background="white",
fieldbackground="white", foreground="black")
elif col=='yellow':
style.configure('Treeview',background="yellow",
fieldbackground="yellow", foreground="black")
else:
style.configure('Treeview',background="black",
fieldbackground="black", foreground="white")
r1 = tk.Radiobutton(my_w, text='Black', variable=r1_v, value='black',
command=lambda:my_upd('black'))
r1.grid(row=2,column=1)
r2 = tk.Radiobutton(my_w, text='White', variable=r1_v, value='white',
command=lambda:my_upd('white'))
r2.grid(row=2,column=2)
r3 = tk.Radiobutton(my_w, text='Yellow', variable=r1_v, value='yellow',
command=lambda:my_upd('yellow'))
r3.grid(row=2,column=3)
my_w.mainloop()
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.