我正在嘗試編寫一個運行我的 HVAC 和許多其他系統的應用程序,在樹莓派上運行。我會第一個承認python和我不是好朋友。但是,我正在使用的傳感器的驅動程序庫似乎與 Java 或其他任何我準備編寫代碼的東西都不兼容。為我工作。本質上,我試圖獲取 getcurrentTH() 函數來更新 GUI 中的 currenttempLabel。我不斷收到錯誤。名稱錯誤:名稱 currenttempLabel 未定義我猜這與我如何嘗試從外部函數調用類內部的標簽有關。任何指針將不勝感激。代碼如下。import mysql.connectorimport timeimport Adafruit_DHT as dhtfrom tkinter import *import randomimport _threadimport threadingstarted = 0currentTemp = 0currentHumidity = 0def getcurrentTH(): while started > 0: global currentTemp global currentHumidity h,t = dht.read_retry(dht.DHT22, 4) currentTemp = ((t *1.8) + 32) currentHumidity = h currenttempLabel['text'] = currentTemp time.sleep(5)def start(): global started started = 1 print("started") print(started) t3 = threading.Thread(target=getcurrentTH) t3.start()def stop(): global started started = 0def quitapp(): exit()class Window(Frame): def __init__(self, master = None): Frame.__init__(self, master) self.master = master self.init_window() def init_window(self): global currentTemp global currentHumidty self.pack(fill=BOTH, expand=1) startButton = Button(self, height = 3, width = 5, bg = "light green", text= "Start", command=start) startButton.place(x=625, y=50) stopButton = Button(self, height =3, width =5, bg = "red", text = "Stop", command=stop) stopButton.place(x=625, y=150) statusLabel = Label(self, text = "Current Status: N/A") statusLabel.place(x=600, y=125) quitButton = Button(self, height =3, width = 5, text = "Quit", command=quitapp) quitButton.place(x=625, y=500) currenttempLabel = Label(self, font = ("Courier",26), text = "No Current Reading" ) currenttempLabel.place(x=50, y=50)root = Tk()root.geometry("800x600")app = Window(root)root.mainloop()
添加回答
舉報
0/150
提交
取消