16

Does anyone know how I can change the color of my ttk.progressBar? It now shows a green color, and I would love to have it blue.

import ttk
self.progressBar = ttk.Progressbar(frame3, length=560, maximum=100, mode='determinate');
self.progressBar.place(x=-5, y=60)
Coryza
  • 231
  • 1
  • 3
  • 12
  • this is a duplicate of [my question](http://stackoverflow.com/questions/13157214/changing-colour-of-ttk-progressbar-elements-in-the-xpnative-theme-python) but hopefully this will get more attention – jbaldwin Nov 22 '12 at 10:54
  • but yeah, you can change the the colour in only one ( the alt) of the windows themes, basically. but using tcl you can change the state, but I'm not sure on that... – jbaldwin Nov 22 '12 at 10:57

4 Answers4

22

You can change the color of a progressbar, but it is tricky. First, you need to understand that if you use the default theme, which is the default theme if you do not specify a theme in the tk.style. Then it will pass all the information it needs to the operating system, which will do the drawing using its style, disregarding style info you passed it. meaning it will draw a Window's style green progressbar on Windows and so on and so forth. What you need to do is change the theme to a custom one that ttk draws. Try the "clam" style, it is one of the best looking styles that ttk allows you to chose from. here is a working adapted excerpt from a script I wrote:

import Tkinter as tk
import ttk as ttk
root = tk.Tk()
frame = tk.Frame(root)
frame.grid()
s = ttk.Style()
s.theme_use('clam')
s.configure("red.Horizontal.TProgressbar", foreground='red', background='red')
ttk.Progressbar(frame, style="red.Horizontal.TProgressbar", orient="horizontal",
                length=600, mode="determinate", maximum=4, value=1).grid(row=1, column=1)
frame.pack()

and here is a picture confirming it works.

Progress bar color

martineau
  • 119,623
  • 25
  • 170
  • 301
clan
  • 353
  • 2
  • 10
  • This way is not pythonic. Think about restoring old style. It's so complicated to just change the color. Tkinter should add an api to change progressbar's state(normal, pause, error) like in win32: https://learn.microsoft.com/zh-cn/windows/win32/controls/pbm-setstate – Keelung Oct 31 '19 at 15:21
  • If I remove the ```theme_use``` the changing of color doesn't work, and infact that process doesn't show any effect, because output like in the picture you've added also gets produced just with using the theme that you've mentioned. – DaniyalAhmadSE Jun 03 '21 at 20:20
  • I tried to use 'aqua' style and this didn't work, but I change it to 'clam' the same as this example and then it works! I was trying this during 1 hour, change the theme and voila! Solved in seconds! Thank you. – Exel Gamboa Feb 01 '23 at 13:54
  • Wow, thank you. Note for anyone else who screwed up like me: You need to call "use_theme" _before_ you configure a style otherwise it doesn't work – Kibi Feb 08 '23 at 15:15
11

You need to use a style to set the progress bar color. The progress bar style elements are:

Element Defines
troughcolor background of full widget
background background of progress bar
darkcolor bottom & right progress bar innermost border
lightcolor top & left bar progress bar innermost border
bordercolor outline border of the progress bar (outside the above border) and the whole widget

If you want the borders to be the same color as the progress bar, you can use this code:

TROUGH_COLOR = 'blue'
BAR_COLOR = 'green'
style.configure("bar.Horizontal.TProgressbar", troughcolor=TROUGH_COLOR, 
                bordercolor=TROUGH_COLOR, background=BAR_COLOR, lightcolor=BAR_COLOR, 
                darkcolor=BAR_COLOR)
martineau
  • 119,623
  • 25
  • 170
  • 301
m herbert
  • 121
  • 1
  • 2
3

I find out that for me it wont work, I don't want to say this is wrong however I have found use [ troughcolor ] to change background and [ background ] for indicator bar

not working for me s.configure("red.Horizontal.TProgressbar", foreground='red', background='red')

my working way s.configure("red.Horizontal.TProgressbar", troughcolor ='gray', background='red')

NirMH
  • 4,769
  • 3
  • 44
  • 69
1

The Progressbar appears to take a style argument. According to the documentation, a style can be used to set the foreground and background colours.

Note: I haven't tried it myself, just pointing you to the relevant docs

mbatchkarov
  • 15,487
  • 9
  • 60
  • 79