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()
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).