The background color of my guizero code is not updating

426 Views Asked by At

The background color of my guizero code is not updating. It starts with grey and stays grey (color options are blue, orange, red) but print statement gives proper value. The temp value is updating normally in GUI. What part of the bg_color assignment incorrect?

from guizero import *
import random   

def read_sensor():
    return random.randrange(3200, 5310, 10) / 100

def read_cpu_temp():
    tFile = open('/sys/class/thermal/thermal_zone0/temp')
    temp = float(tFile.read())
    return temp/1000

def update_label():
    text.value = read_cpu_temp()
    text.value = bg_color()
    # recursive call
    text.after(1000, update_label)  
def bg_color():
    print (read_cpu_temp())
    if read_cpu_temp() < 45.000:
        bg_color = "#00BFFF"
        print ("blue")
    elif 45.000 < read_cpu_temp() < 60.000:
        bg_color = "#FF8C00"
        print ("orange")
    else:
        bg_color = "#FF0000"
        print ("red")  

if __name__ == '__main__':
    app = App(title='Core Temp', height=30, width=100, layout='grid', bg = bg_color())
    title = Text(app, "Temp:", grid=[0, 0], color="white")
    text = Text(app, "xx", grid=[1, 0], color="white")
    text.after(1000, update_label)
    app.display()
2

There are 2 best solutions below

0
On
from guizero import *
import random   

def read_sensor():
    return random.randrange(3200, 5310, 10) / 100

def read_cpu_temp():
    tFile = open('/sys/class/thermal/thermal_zone0/temp')
    temp = float(tFile.read())
    tFile.close()
    return temp / 1000

def update_label():
    temp = read_cpu_temp()
    text.value = temp
    bg_color(temp)
    # recursive call
    text.after(1000, update_label)  
def bg_color():
    global app
    print(temp)
    if temp() < 45.000:
        app.bg = "#00BFFF"
        print ("blue")
    elif 45.000 < temp() < 60.000:
        app.bg = "#FF8C00"
        print("orange")
    else:
        app.bg = "#FF0000"
        print("red")  

if __name__ == '__main__':
    app = App(title='Core Temp', height=30, width=100, layout='grid')
    title = Text(app, "Temp:", grid=[0, 0], color="white")
    text = Text(app, "xx", grid=[1, 0], color="white")
    text.after(1000, update_label)
    app.display()

You need the app being global, being an entry arg of the bg_color function or be declared before the functions, I choosed to make it global. I've optimized the code because you was measuring te temperature always that you needed the temperatre instead of storing it in a variable and use it. This way, your script reads the file less times saving time because RAM access is faster than Disk access.

In the text.after function you can use to app.repeat(time, function).

0
On

I think this is the right way and I got it correct

from guizero import *
import random   

def read_sensor():
    return random.randrange(3200, 5310, 10) / 100

def read_cpu_temp():
    tFile = open('/sys/class/thermal/thermal_zone0/temp')
    temp = float(tFile.read())
    return temp / 1000

def update_label():
    text.value = read_cpu_temp()
    text.value = bg_color()
    # recursive call
    text.after(1000, update_label)  
def bg_color():
    print(read_cpu_temp())
    if read_cpu_temp() < 45.000:
        app.bg = "#00BFFF"
        print ("blue")
    elif 45.000 < read_cpu_temp() < 60.000:
        app.bg = "#FF8C00"
        print("orange")
    else:
        app.bg = "#FF0000"
        print("red")  

if __name__ == '__main__':
    app = App(title='Core Temp', height=30, width=100, layout='grid')
    title = Text(app, "Temp:", grid=[0, 0], color="white")
    text = Text(app, "xx", grid=[1, 0], color="white")
    text.after(1000, update_label)
    app.display()