Search code examples
pythonuser-interfacetkinterbackgroundslider

Can't change background of ttk Scale


I have a very simple program:

import tkinter as tk
from tkinter import ttk

# Create a new style with a red background for the slider
style = ttk.Style()
style.configure('Custom.Horizontal.TScale', background='red')

# Create a new window with a slider widget using the custom style
root = tk.Tk()
slider = ttk.Scale(root, from_=0, to=100, length=200, orient='horizontal', style='Custom.Horizontal.TScale')
slider.pack(pady=20)

root.mainloop()

But the background of the slider remains unchanged. I've already looked at this question: How to set tkinter scale slider's color?

And it didn't work for me. The docs say you need to use a style, so I have. I also tried changing the troughcolor but that didn't work either


Solution

  • But the background of the slider remains unchanged

    You cannot put root=tk.Tk() after style = ttk.Style()

    Move root=tk.Tk() before style = ttk.Style()

    snippet:

    import tkinter as tk
    from tkinter import ttk
    
    root = tk.Tk()
    
    # Create a new style with a red background for the slider
    style = ttk.Style()
    style.configure('Custom.Horizontal.TScale', background='red')
    
    # Create a new window with a slider widget using the custom style
     
    slider = ttk.Scale(root, from_=0, to=100, length=200, orient='horizontal', style='Custom.Horizontal.TScale')
    slider.pack(pady=20)
    
    root.mainloop()
    

    Screenshot:

    enter image description here