0
votes

I am quite new to Python & tkinter but I have a basic GUI which just displays the temperature read from the GPIO. The only thing that I can't get to work is the temperature doesn't update.

If anyone can help, that would be great.

Thanks.

#!/usr/bin/env python3

from tkinter import *
from tkinter import ttk
from tkinter import font
import time
import glob
import os

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

def quit(*args):
    root.destroy()

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.after(0.1)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = round(float(temp_string) / 1000.0,3)
        return temp_c
    root.after(1000, read_temp) 

root = Tk()
root.attributes("-fullscreen", True)
root.configure(background='black')
root.bind("<Escape>", quit)
root.bind("x", quit)

fnt = font.Font(family='Helvetica', size=300, weight='bold')
txt = StringVar()
txt.set(read_temp())
lbl = ttk.Label(root, textvariable=txt, font=fnt, foreground="white", background="black")
lbl.place(relx=0.5, rely=0.5, anchor=CENTER)

root.after(1000, read_temp)

root.mainloop()
1
You should update the StringVar txt inside read_temp() by replacing return temp_c by txt.set(temp_c). Also remove txt.set(read_temp()) as well. - acw1668
Thanks acw1668, thats great. Also, is there an easy way to add " °C" after the temperature reading? - Faz
You can use txt.set(f"{temp_c} °C"). - acw1668
Thank you so much. Do you mind if I ask you one last question? How can I change the colour so <23 is blue, 24-27 is green and >28 is red? - Faz
Refer to my answer. - acw1668

1 Answers

0
votes

In order to update the temperature label lbl with the temperature reading and set its color based on the temperature, you need to:

1. create styles (as you have used ttk.Label) for the colors

style = ttk.Style()
style.configure('Blue.TLabel',  foreground='blue')
style.configure('Green.TLabel', foreground='green')
style.configure('Red.TLabel',   foreground='red')

put the code block before creating the temperature label

2. remove foreground setting when initializing the temperature label

lbl = ttk.Label(root, textvariable=txt, font=fnt, background="black")

3. update the temperature label inside read_temp()

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.after(0.1)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = round(float(temp_string) / 1000.0,3)
        # update temperature label
        txt.set(f'{temp_c:.3f} °C')
        # set its color based on temperature reading
        style = 'Blue.TLabel' if temp_c < 23 else 'Red.TLabel' if temp_c > 28 else 'Green.TLabel'
        lbl.config(style=style)
    root.after(1000, read_temp)