I am trying to set the background of a Treeview widget based on the answers provided here
The issue with this answer is it only covers global styles. In my code i am using 2-3 Treeviews which each need their own style.
In the code provided I want to set the background of the first Treeview to white, and the second Treeview to black.
MCVE:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
def _fixed_map(option, style):
return [elm for elm in style.map("Treeview", query_opt=option) if elm[:2] != ("!disabled", "!selected")]
def set_theme(tv, bg, fg):
style = ttk.Style()
style.theme_use("clam")
style.map("Treeview", foreground=_fixed_map("foreground", style), background=_fixed_map("background", style))
style.configure("Treeview", background=bg, fieldbackground=bg, foreground=fg)
tree1 = ttk.Treeview(root)
tree1.pack(side="left", fill="both", expand=True)
for i in range(10):
tree1.insert("", "end", text=f"t1 Item #{i+1}")
set_theme(tree1, "white", "black")
tree2 = ttk.Treeview(root)
tree2.pack(side="left", fill="both", expand=True)
for i in range(10):
tree2.insert("", "end", text=f"t2 Item #{i+1}")
set_theme(tree2, "black", "white")
root.mainloop()