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)