间歇性错误:AttributeError:'App'对象没有属性equipTemp

1 投票
1 回答
3309 浏览
提问于 2025-04-18 12:14

我最近在玩这个代码,想从我的温度传感器获取读数。

奇怪的是,每次运行这个代码,大约有四分之三的时间我都会遇到这个错误:

追踪记录(最近的调用最后):文件 "/home/pi/Code-working-library
/Temp_and_window_working ds18b20.py",第118行,
app.equipTemp.set(round(read_temp(),1)) 属性错误:'App'对象没有 属性'equipTemp'

这是我的代码:

from Tkinter import *
import os
import glob
import time
import subprocess
import re
import sys
import time
import threading

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

#28-000005c6ba08
#28-000005c70f69
#28-000005c80eb9

#the_count = [1, 2, 3, 4, 5]
#sensors = ['28-000005c6ba08', '28-000005c70f69', '28-000005c80eb9']
sensors = ['28-000005c6ba08'] 

def read_temp_raw():
    catdata = subprocess.Popen(['cat',device_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out,err = catdata.communicate()
    out_decode = out.decode('utf-8')
    lines = out_decode.split('\n')
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_f




##############################################
###########  build window  ###################
##############################################




class App(threading.Thread):

    def _init_(self):
        threading.Thread._init_(self)
        self.start()
    def callback(self):
        self.root.quit()
    def run(self):
        self.root = Tk() #Makes the window
        self.root.wm_title("Temperature Monitor") 
        self.root.minsize(200,100)

        leftFrame = Frame(self.root, width=200, height = 600)
        leftFrame.grid(row=0, column=0, padx=10, pady=2)

        Label(leftFrame, text="Equipment").grid(row=0, column=0, padx=10, pady=2)

        self.equipTemp = StringVar()



        Label(leftFrame, textvariable=self.equipTemp, width=8, justify=RIGHT).grid(row=5, column=0, padx=10, pady=2)
        Label(leftFrame, text="deg F").grid(row=5, column=1, padx=10, pady=2)

        self.root.mainloop() #start monitoring and updating the GUI


##############################################
###########  Start Loop    ###################
##############################################

print "starting app"


app = App()
app.start()


print "app started"


###########################################################
###################  Begin ds18b20 function  ##############
###########################################################



while True:



    for i in sensors:
        base_dir = '/sys/bus/w1/devices/'
        device_folder = glob.glob(base_dir + i)[0]
        device_file = device_folder + '/w1_slave'

        #print i, (read_temp())


        ##################################################
        ##################### END ds18b20 Function  ######
        ##################################################




    app.equipTemp.set(round(read_temp(),1))

    # Wait 30 seconds before continuing
    time.sleep(5)

1 个回答

0

如果这个问题偶尔出现,那就像是经典的“竞争条件”。也就是说,代码在变量还没设置好之前就开始尝试访问这个变量了。

Tkinter这个库本身不支持多线程。虽然我不能百分之百确定你不能在另一个线程中访问IntVar,但很有可能是不能的。

既然你只是读取(短的?)文本文件(或者类似文本文件的设备),我不太确定你是否需要使用多线程。假设你能在大约100毫秒内读取任何一个传感器文件,那其实根本不需要多线程。你可以使用after来定期调用一个函数,读取传感器数据。

撰写回答