Python
Tkinter
Treeview
Assigning default style for Treeview
This is applied to all Treeview widgets.
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.
row height by using style.
style.configure('Treeview', rowheight=100)
Managing style using Radio buttons
Using three RadioButtons we will configure the style of Treeview. We used on StringVar() with default value as 'black'. On click of the radio button we will trigger the function my_upd(col) and we passed colour as parameter.
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 )
Configuring Tkinter Treeview style background foreground color using Radio buttons
VIDEO
Full code is here
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()
Default
Dynamic Creation of Header & Columns in Treeview »
Display MySQL records in Treeview » Pagination of MySQL records in Treeview »
← Displaying MySQL records using Entry or Label ← Delete MySQL record
Treeview
Treeview insert Treeview parent child node
Select -Edit-update MySQL Product table using Treeview »
← Subscribe to our YouTube Channel here