我正在开展一个涉及使用DHT22传感器读取温度和湿度的项目。
我想在GUI上显示这些值,并不断更新GUI上的值。
此外,我想在启动时启动此脚本。
通过将sensorstartup.desktop文件添加到~./ config / autostart,我已经实现了启动脚本并显示GUI,文件由指向sudo python /home/pi/GUISENSOR.py的sh脚本组成
我遇到的问题是它不会在启动时更新GUI,GUI会出现但没有更新,但是如果我手动运行它sudo python /home/pi/GUISENSOR.py,它会正确运行和更新
任何建议都将不胜感激。
以下是代码示例
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)
#Left Frame and its contents
leftFrame = Frame(self.root, width=200, height = 600)
leftFrame.grid(row=0, column=0, padx=10, pady=2)
Label(leftFrame, text="Equiptment").grid(row=0, column=0, padx=10, pady=2)
self.equipTemp = DoubleVar()
Label(leftFrame, textvariable=self.equipTemp).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
app = App()
app.start()
# Continuously append data
while(True):
# Run the DHT program to get the humidity and temperature readings!
output = subprocess.check_output(["./temp_sensor"]);
matches = re.search("Temp =\s+([0-9.]+)", output)
if (not matches):
time.sleep(3)
continue
tempC = float(matches.group(1))
tempF = tempC * 1.8 + 32
tempF = round(tempF)
tempF = int(tempF)
print "Temperature: %.1f F" % tempF
if tempC > max_temp:
oos += 1
elif tempC < min_temp:
oos += 1
else:
oos = 0
if oos > bad_readings:
oos = 0
emailer(email_addr, room_subject + " " + str(tempF) + " degF " )
app.equipTemp.set(tempF)
# Wait 30 seconds before continuing
time.sleep(30)