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

The Treeview widget in Python tkinter is used for creating table-like interfaces. To customize the appearance of Treeview headings, we use the ttk.Style() class which allows us to modify properties like background color, foreground color, and font styles.

Basic Syntax

To style Treeview headings, use the following approach ?

import tkinter as tk
from tkinter import ttk

# Create style object
style = ttk.Style()

# Configure heading style
style.configure('Treeview.Heading', background='color', foreground='color')

Example: Customizing Treeview Heading Style

In this example, we create a Treeview widget and customize the heading background color using ttk.Style() ?

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

# Create an instance of tkinter frame
win = Tk()
win.title("Treeview Heading Style Example")

# Set the size of the tkinter window
win.geometry("700x350")

# Create and configure style
s = ttk.Style()
s.theme_use('clam')

# Configure the style of Heading in Treeview widget
s.configure('Treeview.Heading', background="green3", foreground="white", font=("Arial", 12, "bold"))

# Add a Treeview widget
tree = ttk.Treeview(win, columns=("c1", "c2"), show='headings', height=8)

# Configure columns
tree.column("#1", anchor=CENTER, width=100)
tree.heading("#1", text="ID")
tree.column("#2", anchor=CENTER, width=150)
tree.heading("#2", text="Car Brand")

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

tree.pack(pady=20)

win.mainloop()

Advanced Styling Options

You can customize multiple properties of the Treeview heading ?

from tkinter import *
from tkinter import ttk

win = Tk()
win.title("Advanced Treeview Styling")
win.geometry("600x400")

# Create style object
style = ttk.Style()
style.theme_use('clam')

# Configure multiple heading properties
style.configure('Treeview.Heading',
                background='#2E86C1',    # Blue background
                foreground='white',      # White text
                font=('Helvetica', 11, 'bold'),  # Bold font
                relief='raised',         # Raised border effect
                borderwidth=2)           # Border width

# Create Treeview
tree = ttk.Treeview(win, columns=('Name', 'Age', 'City'), show='headings')

# Set column properties
tree.heading('Name', text='Full Name')
tree.heading('Age', text='Age')
tree.heading('City', text='City')

tree.column('Name', width=150, anchor='w')
tree.column('Age', width=80, anchor='center')
tree.column('City', width=120, anchor='w')

# Add sample data
data = [
    ('Alice Johnson', '28', 'New York'),
    ('Bob Smith', '34', 'London'),
    ('Carol Davis', '25', 'Paris'),
    ('David Wilson', '41', 'Tokyo')
]

for row in data:
    tree.insert('', 'end', values=row)

tree.pack(pady=20, padx=20)

win.mainloop()

Common Style Properties

Property Description Example Values
background Background color 'blue', '#FF5733'
foreground Text color 'white', '#000000'
font Font family and style ('Arial', 12, 'bold')
relief Border style 'raised', 'sunken', 'flat'

Output

Executing the above code will display a window containing a table with customized heading styles. The headings will have a blue background, white text, and bold font styling.

Conclusion

Use ttk.Style().configure('Treeview.Heading', ...) to customize Treeview heading appearance. You can modify background color, text color, font, and border properties to create professional-looking table interfaces in your tkinter applications.

---
Updated on: 2026-03-25T22:26:26+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements