How to change the background color of a Treeview in Tkinter?

The Treeview widget is designed to display the data in a hierarchical structure. It can be used to display the directories, child directories or files in the form of a list. The items present in the Listbox are called Listbox items.

The treeview widget includes many properties and attributes through which we can change or modify its default properties. We can change the background of a treeview widget by defining the 'background' property in the constructor.


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

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

# Set the size of the window

# Create a Listbox widget
lb = Listbox(win, width=100, height=10, background="purple4", foreground="white", font=('Times 13'),selectbackground="black")

# Select the list item and delete the item first
# Once the list item is deleted, we can insert a new item in the listbox
def edit():
   for item in lb.curselection():
      lb.insert("end", "foo")

# Add items in the Listbox
lb.insert("end", "item1", "item2", "item3", "item4", "item5")

# Add a Button To Edit and Delete the Listbox Item
ttk.Button(win, text="Edit", command=edit).pack()



If we run the above code, it will display a window with a treeview widget having a distinct background color and some items in it.

Updated on: 05-Aug-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started