0

I'm coding a program (Python 3.6 on Spyder) that will analyse some reports. When the analysis is finished, i click on the exit button, but the code is still running on the console Ipython, how can i fix it ?

I tried with window.destroy() ; window.quit(); window.exit() but no way the code still running on the Ipython console.

# MAIN

window= Tk()

window.title("Cash Feed Upload Analysis")

window.configure(bg="SteelBlue1")

Label(window, text="Voici les rapports du jour:", bg="SteelBlue1",fg="black", font="none 14 bold").grid(row=1,column=0,sticky=W)

Button(window, text="Select reports", width=12, command=select).grid(row=3,column=0,sticky=W)

output= Text(window,width=90, height=12, wrap=WORD, background="white")

output.grid(row=5,column=0, columnspan=2, sticky=W)

window.mainloop()
window.destroy

Explaining picture

Thanks in advance.

Tom92
  • 5
  • 1
  • 3

2 Answers2

0

You have not defined any exit button, So, I assume that you are taking about the close(X) button on Tk() window.just call exit(0) on close. To handle functions on closing the window are explained here: [How do I handle the window close event in Tkinter?

You can use following code:

def on_closing():
    exit(0)
    root.destroy()
window=Tk()
#Your code
root.protocol("WM_DELETE_WINDOW", on_closing)
window.mainloop()

If you are planning to have one more separate button for "exit" you can use:

def stop():
    exit(0)

window=Tk()
button=Button(window,text="exit",command=stop)
button.pack()
#Your code
window.mainloop()
Aravind
  • 1
  • 2
  • Hi Aravind, thanks for your answer. Yes, indeed i'm talking about the close(X) button on Tk window. I've put what you told me but the code is still running on the Ipython console. I have to clic on the stop( square) button to stop the execution. – Tom92 Apr 23 '18 at 12:37
  • def on_closing(): if messagebox.askokcancel("Quit"," want to quit ?"): window.destroy() window= Tk() window.title("Cash Feed Upload Analysis") window.configure(bg="black") Label(window, text="Rapports du jour:", fg="red",bg="SteelBlue1", font="none 14 bold").grid(row=6,column=1,sticky=W) Button(window, text="Select reports", width=12, command=select).grid(row=6,column=0,sticky=W) output= Text(window,width=80, height=12, wrap=WORD, background="white") output.grid(row=7,column=0, columnspan=4, sticky=W) window.protocol("WM_DELETE_WINDOW",on_closing) window.mainloop() – Tom92 Apr 23 '18 at 12:39
  • Hi Tom, are you using background function that runs infinite loop..? Then that is the root cause of this as the infinite loop will not give the execution back to other statements unless it is a separate thread. – Aravind Apr 24 '18 at 11:05
0

Try to use window.quit().

def on_closing():
    window.destroy()
    window.quit()
window=Tk()
window.protocol("WM_DELETE_WINDOW", on_closing)
window.mainloop()

Hope it helps

Salem
  • 401
  • 5
  • 15