How handle unexpected Key press How handle unexpected Key press tkinter tkinter

How handle unexpected Key press


A brute force solution would be to check whether root has an L2 attribute or not

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')

A more elegant approach would be to disable the save button on startup and only enable it after the cal function has been executed.

I am not very familiar with Raspberry Pi implementations, so this is only a rough sketch on how to achieve the button disabling:By the looks of it, the buttons are "wired in" via the GPIO.add_event_detect functions.

So i would remove the sav-callback from the main script and dynamically add it after the cal script, something like that:

# [...] 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 hereroot.after(500, update)root.mainloop()