0

I was recently trying to change the style of a ttk.Progressbar and found the style will only apply after a call to s.theme_use. I cannot find any documentation on why these behavior occurs, but using a theme like clam will allow the custom style to be applied to the widget.

Why does this need to be called in order for the styles to apply?

Another SO post talks about it some as well.

Edit: apparently an example is needed?

This has to be hand jammed as code exists on another system, please read comments...

import tkinter as tk
import tkinter.ttk as ttk

TC = 'gray60'
BC = 'steelblue2'

s = ttk.Style()
s.configure('U.Horizontal.TProgressbar', throughcolor=TC, bordercolor=TC,
            background=BC, lightcolor=BC, darkcolor=BC) # ref TKCommands doc

if __name__ == '__main__':

    root = tk.Tk()
    pb = ttk.Progressbar(root, style='U.Horizontal.TProgressbar', orient='horizontal', mode='determinate')
    pb.pack(fill=tk.BOTH, expand=True)

    # then add a button that calls a function that calls the pb.step(1)

The above snippet never changes the widget, its still the default Windows OS "green", but adding another line and it works:

import tkinter as tk
import tkinter.ttk as ttk

TC = 'gray60'
BC = 'steelblue2'

s = ttk.Style()
s.theme_use('clam')  # added
s.configure('U.Horizontal.TProgressbar', throughcolor=TC, bordercolor=TC,
            background=BC, lightcolor=BC, darkcolor=BC) # ref TKCommands doc

if __name__ == '__main__':

    root = tk.Tk()
    pb = ttk.Progressbar(root, style='U.Horizontal.TProgressbar', orient='horizontal', mode='determinate')
    pb.pack(fill=tk.BOTH, expand=True)

    # then add a button that calls a function that calls the pb.step(1)
pstatix
  • 3,611
  • 4
  • 18
  • 40
  • Did you pass in a `style` argument when creating the progressbar? I says that you have to do that in [this unofficial documentation](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-Progressbar.html). – TheLizzard Jul 19 '21 at 19:57
  • @TheLizzard Yes, the `style` keyword was declared. I assure you, couldn't figure it out until I came another SO post that suggested it but gave no explanation as to why a theme has to be used. – pstatix Jul 19 '21 at 20:07
  • Please provide a [mcve]. – Bryan Oakley Jul 19 '21 at 20:07
  • I think this is because the Windows default theme does not allow changing the colors of the progressbar (it's probably based on images) while clam allows it. Not all themes have the same degree of customizability. – j_4321 Jul 20 '21 at 08:29

1 Answers1

0

Theme objects are not automatically applied on creation. They never have been (other than for the default OS-specific theme, of course; that's chosen so that widgets look native or nearly so). This does mean that if you have an application that supports changing themes, you can just create all the themes up front without having to worry about the creation process interfering with anything.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215