Search code examples
pythonimageimage-processingtkinterdestroy

Using destroy in tkinter the root disappear but the script keeps running


I have done a script that process some images and at the end, it asks you where do you want to save the file. I made it using tkinter (code below). It works fine, the only problem that I have is:

I have an exit button linked with the function destroy and when I click it, the root is closed, but the script is still running, no error appear and I have to stop it manually. If I use quit the script is stopping but the root window freezes and kernel error appear restarting python. I tried to use sys.exit() after the root.mainloop but the script is not going out of the loop. Hope you can help, thank you.

I am using Spider, python 3.7.6, Ipython 7.19.0

root = Tk() 
root.geometry('200x150') 


  
# function to call when user press 
# the save button, a filedialog will 
# open and ask to save file 
def save(): 
    filename2 = filedialog.asksaveasfile(mode='wb+',defaultextension=".tif", title="Choose filename")
    if not filename2:
        return
    
    imwrite(filename2, stack, imagej=True)# 
    # root.destroy()

    
button_save = Button(root, text = 'Save', command = lambda : save()) 
button_save.grid(row=1,column=2) 
button_exit=Button(root,text='Exit program', command=root.destroy)
button_exit.grid(row=5,column=2) 


root.mainloop() 

Solution

  • Ok, I solved it, perhaps it helps someone in the future.

    I defined a new function to quit as follows:

    def _quit():
        root.quit()     
        root.destroy()
        
    button_exit=Button(root,text='Exit program', command=_quit)