1

I don't seem to be able to find the answer to this. I am using several Tkinter Treeviews in my program. When I change the style of one, it changes all of them. What Am I missing? This is the code I have written for one, its repeated four times (the others with different colours)

style = ttk.Style()
#style.theme_use("default")
style.configure("Treeview",background="Black", foreground="White",fieldbackground="red")
style.map('Treeview', background=[('selected','#3c3737')],foreground=[('selected','white')])
my_tree = ttk.Treeview(my_canvas2,height=1000)

2 Answers 2

5

You can create custom widget styles by using style.configure("<custom_name>.<widget_type>".... So, if you wanted to create a custom "Treevew" style, you would use style.configure("MyCustom.Treeview"...).

You then would create a ttk widget and pass the custom widget style as the style argument, for example my_treeview = ttk.Treeview(master, style="MyCustom.Treeview"). Here is an example program that creates two different ttk.TreeViews, with different styles:

import tkinter
from tkinter import ttk

w = tkinter.Tk()

style = ttk.Style()
style.configure("Custom1.Treeview",background="Black", foreground="White",fieldbackground="red")
style.map('Custom1.Treeview', background=[('selected','#3c3737')],foreground=[('selected','white')])

style.configure("Custom2.Treeview",background="Greed", foreground="Purple",fieldbackground="pink")
style.map('Custom2.Treeview', background=[('selected','#3c3737')],foreground=[('selected','white')])

my_tree1 = ttk.Treeview(w, height=1000, style="Custom1.Treeview")
my_tree1.pack(side="left")

my_tree2 = ttk.Treeview(w, height=1000, style="Custom2.Treeview")
my_tree2.pack(side="right")

w.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

0

I hope I would help you:

from tkinter import ttk, CENTER, EW
w = tkinter.Tk()
style_wear = ttk.Style()
style_wear.configure("fortree1.Treeview",background="yellow", foreground="black",fieldbackground="red")
style_wear.map('fortree1.Treeview', background=[('selected','green')],foreground=[('selected','black')])
tree02 = ttk.Treeview(w, column=("c1", "c2","c3",), show='headings', height=5,style="fortree2.Treeview")
tree02.column("# 1", anchor=CENTER, width=40)
tree02.heading("# 1", text="S/N")
tree02.column("# 2", anchor=CENTER, width=170)
tree02.heading("# 2", text="Nature of damage")
tree02.column("# 3", anchor=CENTER, width=65)
tree02.heading("# 3", text="Start")
tree02['show']='headings'
tree02.grid(row=1,column=0,columnspan=4,sticky=EW)
tree02.insert("", 'end', values=("a","a","a"))
tree02.insert("", 'end',values=("b","b","b",))
style_measurement = ttk.Style(w)
style_measurement.configure("fortree2.Treeview",background="Green", foreground="red",fieldbackground="pink")
style_measurement.map('fortree2.Treeview', background=[('selected','blue')],foreground=[('selected','white')])
tree1 = ttk.Treeview(w, show='headings', height=10,column=("c1", "c2","c3"),style="fortree1.Treeview")
tree1.column("# 1", anchor=CENTER, width=185)
tree1.heading("# 1", text="From the bottom tube")
tree1.column("# 2", anchor=CENTER, width=100)
tree1.heading("# 2", text="Original")
tree1.column("# 3", anchor=CENTER, width=100)
tree1.heading("# 3", text="Measured")
tree1.insert("", 'end',values=("a","a","a"))
tree1.insert("", 'end',values=("b","b","b"))
tree1['show']='headings'
tree1.grid(row=2,column=0,columnspan=3)
w.mainloop()```

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.