Search code examples
pythontkintercolorsprogress-barttk

ttk Progressbar color - can you change with state?


I have am building a Python 3 application using tkinter on a Windows 10 machine. I finished building it out, and now need to add a progress bar for a new feature. The progress bar must change color depending on its value.

My application is already all built using the vista theme, so it is not an option to change to clam theme, as I will have to adjust the GUI for every other element.

I found a discussion here that alludes to the use of state to change the color of the bar. I have tried:

progressBar.state(statespec=["user3"])

to try to turn my bar red, without success. It stays stubbornly green. Is there a different way to set state for these bars to get the desired effect?

Because I am using the vista theme for the rest of my GUI, I cannot simply configure a style. Any help is greatly appreciated.


Solution

  • For anyone having a similar issue, I recommend checking out the answer here about changing the color of an EntryBox.

    I used a similar solution to get exactly what I wanted (again, I am using Python 3 on a Windows 10 machine):

    from tkinter import *
    from tkinter import ttk
    
    root = Tk()
    style = ttk.Style()
    style.element_create("color.pbar", "from", "clam")
    style.layout("ColorProgress.Horizontal.TProgressbar",
                         [('Horizontal.Progressbar.trough',
                          {'sticky': 'nswe',
                           'children': [('Horizontal.Progressbar.color.pbar',
                             {'side': 'left', 'sticky': 'ns'})]})])
    style.configure("ColorProgress.Horizontal.TProgressbar", background='orange')
    myProgress=ttk.Progressbar(root,orient='horizontal',
                                             length=500,mode='determinate',
                                             style='ColorProgress.Horizontal.TProgressbar')