Displaying Temperature from DHT11 in GUI - Automatic Refresh?

后端 未结 1 760
日久生厌
日久生厌 2021-01-24 16:08

Newbie to Python, started off messing with a DHT11 Temp/Humidity Sensor, a Raspberry Pi 3, and Python 3.

I am using the standard Adafruit DHT11 Library for Python.

相关标签:
1条回答
  • 2021-01-24 16:52

    Create the Label once at the start of the program, and save a reference:

    the_label = Label (win, text="", fg="black", bg="white", font="36")
    the_label.grid(row=0, column=0)
    

    Next, create a function that gets the value and updates the 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 a new function that calls this function, and then schedules itself t be called again after a delay:

    def read_every_second():
        READ()
        root.after(1000, read_every_second)
    

    Finally, call read_every_second once at the start of your program. It will then run until your program exits.

    0 讨论(0)
提交回复
热议问题