简体   繁体   中英

Why does my progress bar in tkinter only change colour when value is used as an option over variable?

I'm using a progress bar in tkinter like a meter which indicates the signal strength being received. I would like to change the colour of my progress bar so that it turns red when below a threshold value and green when above it. When I use "value" in "ttk.Progressbar" along with "ttk.Style" I can change the colour of the bar. However the value being displayed should change as the signal strength changes and when I use "variable" instead it remains grey. The code which highlights my problem is shown below. The argument "root" is from another class which creates a frame so I can position this progress bar on a grid with other labels. The first example does change colour.

from tkinter import *
from tkinter import tkk
class Progressbar():
    def __init__(self, root):

        self.s = ttk.Style()
        self.s.theme_use("clam")
        self.s.configure("colour.Horizontal.TProgressbar", foreground="green", background="green")

        self.pb = ttk.Progressbar(root, orient = "horizontal", length = 140, mode = "determinate", value = 20, maximum = 100, style = "colour.Horizontal.TProgressbar").grid(row=3, column=1)

This next example displays the value but does not change colour like the previous code. This is the problem I am having. I call the function in another file and input a float number. This works as expected and if I input "50.0", for example, it displays the value on the progress bar but the colour remains grey unlike last time. The only difference between the two blocks of code is that in the second one I use "variable" instead of "value" and set the value using a variable class.

from tkinter import *
from tkinter import tkk
class Progressbar():
    def __init__(self, root):

        self.s = ttk.Style()
        self.s.theme_use("clam")
        self.s.configure("colour.Horizontal.TProgressbar", foreground="green", background="green")

        self.progbarval = DoubleVar()

        self.pb = ttk.Progressbar(root, orient = "horizontal", length = 140, mode = "determinate", variable = self.progbarval, maximum = 100, style = "colour.Horizontal.TProgressbar").grid(row=3, column=1)

    def set_progbarval(self, val1):

        self.progbarval.set(val1)

I expected both pieces of code to behave the same way but they did not. Please could someone explain why this method for changing the colour of the progress bar does not work and what possible solutions there are for my problem. Any help would be appreciated, thanks.

For some reason, when you put them on the same window it doesn't work but separately try using this line (it worked for me):

self.s.configure("colour.Horizontal.TProgressbar", foreground="green", background="green", theme = "clam")

rather than:

self.s.theme_use("clam")
self.s.configure("colour.Horizontal.TProgressbar", foreground="green", background="green")

I realized I forgot to put root.update() in my main code so it wasn't updating. The progress bar changed colour after I did this. Please can a moderator delete this post on behalf of me as it was just an error on my part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM