How to edit the style of a heading in Treeview (Python ttk)?


Python Treeview widget is introduced for creating a Table look-like GUI in application. It includes many inbuilt features and functions which can be used to configure the properties. However, to configure the style of a tkinter widget, we generally refer to use ttk themed widget. This allows you to edit the style such as background color, foreground color, and other properties of the treeview widget as well.

Example

In this example, we will create an instance of the ttk style widget and then configure the style of heading by passing 'Treeview.Heading' as the style parameter.

# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame
win= Tk()

# Set the size of the tkinter window
win.geometry("700x350")
s = ttk.Style()
s.theme_use('clam')

# Configure the style of Heading in Treeview widget
s.configure('Treeview.Heading', background="green3")

# Add a Treeview widget
tree= ttk.Treeview(win, column=("c1", "c2"), show= 'headings', height= 8)
tree.column("# 1",anchor=CENTER)
tree.heading("# 1", text= "ID")
tree.column("# 2", anchor= CENTER)
tree.heading("# 2", text= "FName")

# Insert the data in Treeview widget
tree.insert('', 'end',text= "1",values=('1','Honda'))
tree.insert('', 'end',text= "2",values=('2', 'Hundayi'))
tree.insert('', 'end',text= "3",values=('3', 'Tesla'))
tree.insert('', 'end',text= "4",values=('4', 'Wolkswagon'))
tree.insert('', 'end',text= "5",values=('5', 'Tata'))
tree.insert('', 'end',text= "6",values=('6', 'Renault'))
tree.insert('', 'end',text= "7",values=('7', 'Audi'))
tree.insert('', 'end',text= "8",values=('8', 'BMW'))

tree.pack()

win.mainloop()

Output

Executing the above code will display a window containing a table with a customized heading background color.

Updated on: 08-Jun-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements