1 回答
TA贡献1839条经验 获得超15个赞
强力解决方案是检查 root 是否具有 L2 属性
from tkinter import messagebox
def sav(channel):
if hasattr(root, 'L2'):
global rangeh, offset, fullScale
file = open("/home/pi/data_log.txt", "w")
if os.stat("/home/pi/data_log.txt").st_size == 0:
file.write("rangeh,offset,Full_Scale,\n")
file.write(str(rangeh) + "," + str(offset) + "," + str(fullScale))
file.flush()
root.L2.destroy()
else:
messagebox.showinfo('Unable to save', 'No data was generated yet')
更优雅的方法是在启动时禁用保存按钮,仅在执行 cal 函数后才启用它。
我对 Raspberry Pi 的实现不太熟悉,因此这只是如何实现按钮禁用的粗略草图:从外观上看,按钮是通过 GPIO.add_event_detect 函数“连接”的。
所以我会从主脚本中删除 sav-callback 并在 cal 脚本之后动态添加它,如下所示:
# [...] beginning of your script [...]
def cal(channel):
# [...] original body of cal function [...]
activate_save_button()
def activate_save_button():
GPIO.add_event_detect(12, GPIO.RISING, callback=sav, bouncetime=1000)
def deactivate_save_button():
GPIO.remove_event_detect(12)
def sav(channel):
# [...] original body of sav function [...]
# remove save button functionality after saving
deactivate_save_button()
def update():
""" function for continuous show value in every 500ms in tkinter window"""
GPIO.add_event_detect(5, GPIO.RISING, callback=cal, bouncetime=1000)
# line with callback=sav is deleted here
root.after(500, update)
root.mainloop()
添加回答
举报