newbie python, started off messing dht11 temp/humidity sensor, raspberry pi 3, , python 3.
i using standard adafruit dht11 library python.
reading gpio 27
i able display temperature in gui window fine. stuck on how have gui update/refresh temperature @ set rate "live" display of current temperature. right now, can changes gui if close , reopen script. see code below:
tkinter import * import tkinter.font import adafruit_dht temp = 0 win = tk() win.title("temperature") win.geometry("100x100") def read(): global temp humidity, temperature = adafruit_dht.read_retry(11, 27) temp = temperature * 9/5.0 + 32 label (win, text=str(temp), fg="black", bg="white", font="36").grid(row=0, column=0) if (temp >= 0): read() mainloop()
create label
once @ start of program, , save reference:
the_label = label (win, text="", fg="black", bg="white", font="36") the_label.grid(row=0, column=0)
next, create function gets value , updates label:
def read(): global temp humidity, temperature = adafruit_dht.read_retry(11, 27) temp = temperature * 9/5.0 + 32 the_label.configure(text=str(temp))
next, create new function calls function, , schedules t called again after delay:
def read_every_second(): read() root.after(1000, read_every_second)
finally, call read_every_second
once @ start of program. run until program exits.