为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 120 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号