你好,我讨厌到这里结束,但是我自己不能解决这个问题。
我已经使用tkinter for UI运行了python脚本。它已连接到蓝牙设备和wifi插头。 BT提供温度和湿度,一旦达到一定的温度/湿度值,插头就会打开抽气功能。不幸的是,有时BT设备会给出NoneType值,或者无法连接到wifi插头。因此,脚本崩溃。我已经设置了一个bash脚本,该脚本会自动重新启动python脚本,但这似乎并没有更新tkinter窗口并使它重新运行!我该如何实现?谢谢!
import pandas as pd
import time
import datetime
import pyHS100
from pyHS100 import SmartPlug
from beewi_smartclim import BeewiSmartClimPoller
import datetime as dt
from tkinter import *
from PIL import ImageTk, Image
plug_temp = SmartPlug('192.168.0.21')
result = time.strftime("%I:%M")
env_exp = []
window = Tk()
window.title('green room')
lab_text = Label(window, width = 10)
lab_text.grid(column=0, row=0)
lab = Label(window, width = 10)
lab.grid(column=0, row=1)
lab_h_text = Label(window, width = 10)
lab_h_text.grid(column=1, row=0)
lab_h = Label(window, width = 10)
lab_h.grid(column=1, row=1)
lab_t_text = Label(window, width = 10)
lab_t_text.grid(column=2, row=0)
lab_t = Label(window, width = 10)
lab_t.grid(column=2, row=1)
img_on = ImageTk.PhotoImage(Image.open('/home/pi/on.png'))
img_off = ImageTk.PhotoImage(Image.open('/home/pi/off.png'))
lab_plug_text = Label(window, width = 10)
lab_plug_text.grid(column=3, row=0)
lab_plug = Label(window, width = 50)
lab_plug.grid(column=3, row=1)
def UI():
b = BeewiSmartClimPoller("F0:C7:7F:85:B4:79")
time_day = dt.datetime.now().strftime('%H:%M:%S')
temp_c = b.get_temperature()
humidity = b.get_humidity()
b.update_sensor()
env = [temp_c, humidity, time_day]
env_exp.append(env)
df = pd.DataFrame(env_exp)
df.to_csv ('/home/pi/env_export.csv', index = False, header=['Temp', 'H', 'Time'])
temp = (temp_c,'°C')
hum = (humidity,'%')
if temp_c > 25 or humidity > 48:
plug_temp.turn_on()
else:
plug_temp.turn_off()
lab_text.config(text = 'Time')
lab.config(text = time_day)
lab_h_text.config(text = 'Humidity')
lab_h.config(text = hum)
lab_t_text.config(text = 'Temperature')
lab_t.config(text = temp)
lab_plug_text.config(text = 'Status')
if plug_temp.state == 'ON':
lab_plug.config(image = img_on)
else:
lab_plug.config(image = img_off)
window.after(120000, UI)
UI()
window.mainloop()
和bash代码:
until python3 flowerbeewi.py; do
echo "Crashed with exit code $?/ restarting..." >&2
sleep 3
done