Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to change the background color of a Treeview in Tkinter?
The Treeview widget in Tkinter is designed to display data in a hierarchical structure, such as directories, subdirectories, or files. You can customize its appearance by changing the background color using styling methods.
Understanding Treeview Styling
Unlike basic Tkinter widgets, the Treeview widget requires the ttk.Style() class to change its background color. The background property is controlled through style configuration ?
Example − Changing Treeview Background Color
import tkinter as tk
from tkinter import ttk
# Create main window
root = tk.Tk()
root.title("Treeview Background Color")
root.geometry("600x400")
# Create a style object
style = ttk.Style()
# Configure the Treeview style
style.configure("Treeview",
background="lightblue",
foreground="black",
fieldbackground="lightblue")
# Configure selected item style
style.configure("Treeview.Heading",
background="darkblue",
foreground="white")
# Create Treeview widget
tree = ttk.Treeview(root, columns=("Name", "Type"), show="tree headings")
# Configure column headings
tree.heading("#0", text="Item")
tree.heading("Name", text="Name")
tree.heading("Type", text="Type")
# Insert sample data
tree.insert("", "end", text="Documents", values=("Documents", "Folder"))
tree.insert("", "end", text="Pictures", values=("Pictures", "Folder"))
tree.insert("", "end", text="readme.txt", values=("readme.txt", "File"))
# Pack the treeview
tree.pack(fill="both", expand=True, padx=10, pady=10)
root.mainloop()
Advanced Styling with Multiple Themes
You can create custom themes and apply different background colors for various states ?
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Advanced Treeview Styling")
root.geometry("650x450")
# Create style object
style = ttk.Style()
# Set theme
style.theme_use('clam')
# Configure custom colors
style.configure("Custom.Treeview",
background="lightgreen",
foreground="darkgreen",
fieldbackground="lightgreen",
selectbackground="darkgreen",
selectforeground="white")
# Configure heading style
style.configure("Custom.Treeview.Heading",
background="darkgreen",
foreground="white",
font=('Arial', 10, 'bold'))
# Create Treeview with custom style
tree = ttk.Treeview(root, style="Custom.Treeview")
# Configure columns
tree["columns"] = ("Size", "Modified")
tree.column("#0", width=200)
tree.column("Size", width=100)
tree.column("Modified", width=150)
# Set headings
tree.heading("#0", text="File/Folder")
tree.heading("Size", text="Size")
tree.heading("Modified", text="Last Modified")
# Add sample data
tree.insert("", "end", text="Projects", values=("--", "2024-01-15"))
projects = tree.insert("", "end", text="Documents", values=("--", "2024-01-10"))
tree.insert(projects, "end", text="report.pdf", values=("2.1 MB", "2024-01-08"))
tree.insert(projects, "end", text="notes.txt", values=("1.5 KB", "2024-01-05"))
tree.pack(fill="both", expand=True, padx=15, pady=15)
root.mainloop()
Key Style Properties
| Property | Description | Example Value |
|---|---|---|
background |
Overall background color | "lightblue" |
fieldbackground |
Background of data area | "white" |
selectbackground |
Selected item background | "blue" |
foreground |
Text color | "black" |
Conclusion
Use ttk.Style() to change Treeview background colors effectively. Configure both general background and field background properties for complete customization. Custom styles allow you to create professional-looking hierarchical data displays.
