How do I use ttk.style on ttk.scale the right way?

Viewed 931

I am trying to customize a ttk.Scale using a ttk.Style. I don't really know, how to apply my Style to my Scale. Right now I am using this:

style = ttk.Style()
style.configure(".", background="#505050")
scale = ttk.Scale(self.root, from_ = 100, to = 0, orient=tk.VERTICAL)

This works just fine, but I don't really understand, whats going on with the "." .

I also tried something like this:

style = ttk.Style()
style.configure("myStyle", background="#505050")
scale = ttk.Scale(self.root, from_ = 100, to = 0, orient=tk.VERTICAL, style="myStyle")

But I am getting this error:

Layout Vertical.myStyle.TScale not found

This seems to work for buttons for example, but apparently not for scales:

style = ttk.Style()
style.configure("myStyle", background="#505050")
scale = ttk.Scale(self.root, from_ = 100, to = 0, orient=tk.VERTICAL, style="myStyle.TScale")

But I am also getting the same error.

All in all I am pretty confused on how to handle ttk.Style with ttk.Scale.

How do I apply a ttk.Style to a ttk.Scale the right way?

2 Answers

You just need to put the "TScale" and it will work

from tkinter import ttk, Tk

window = Tk()
style = ttk.Style()
style.configure("TScale", background="#505050")
scale = ttk.Scale(window, from_=0, to=100, value=0, orient="vertical", style="TScale")
scale.pack()

window.mainloop()

If you're styling a horizontal slider, use

style.configure('myStyle.Horizontal.TScale', background="red")

If you're styling a vertical slider, use

style.configure('myStyle.Vertical.TScale', background="red")

Now myStyle acts as a custom style and can be attached to a slider to modify the looks of the respective slider object (Horizontal or Vertical).

Related