How to change ttk.progressBar color in python

梦想的初衷 提交于 2019-11-29 04:44:13

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 working

Gilad Langer

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')

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

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)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!