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()
添加回答
舉報