【发布时间】:2019-06-25 02:26:44
【问题描述】:
我正在尝试制作某种恒温器。为此,我使用了带有 DHT22 温度传感器的 Pi3 和 Python3。
我需要的是轮询温度并自行更新相应的变量。
尝试使用任何类型的 While True: 语句执行此操作会导致我正在测试的 gui 无法打开。
我迷路了(是的,这段代码是从其他人那里盗用的。哈哈)
#! python3
import time
import RPi.GPIO as GPIO
import string
import tkinter
import tkinter.ttk
import Adafruit_DHT
from tkinter import messagebox
from tkinter import *
root = Tk()
root.title('PiTEST')
root.configure(background='black')
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
sensor = Adafruit_DHT.DHT22
pin = 4
def PRINTTEST():
print(temperature, humidity)
TESTTEXT = Label(root,text="TESTING",fg="white",bg="black",font='Consolas 20 bold')
TESTTEXT.grid(row=1,column=1,sticky="W,S,E")
B1 = tkinter.Button(root,bd=5,text="TEST",bg="gray",fg="white",command=PRINTTEST,height=4,width=20)
B1.grid(row=2,column=1,sticky="N,S,E,W",padx=8,pady=8)
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
temperature = temperature * 9/5.0 + 32
root.mainloop()
GPIO.cleanup()
【问题讨论】:
-
你不能使用 While 循环,因为这不会让你的 root.mainloop() 启动。查看函数 root.after(),它可以持续更新变量
标签: python-3.x loops variables temperature