亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python - 從函數外部更新窗口類中的標簽文本

Python - 從函數外部更新窗口類中的標簽文本

料青山看我應如是 2021-06-04 09:08:56
我正在嘗試編寫一個運行我的 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()
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號