Why is exit command is not quitting sometimes in python tkinter exe?

506 Views Asked by At

In some os's, When you make a tkinter window and put it to mainloop, and make a button for exit and convert it to exe (with pyinstaller) like this:

from tkinter import *

def exit_():
    window.destroy()
    exit()

window = Tk()
text = Label(window, text = "Hello World")
text.pack()
window.protocol('WM_DELETE_WINDOW', exit_)
window.mainloop()

If you use the built-in exit() command, then sometimes the window will not get closed.

Is there an Answer for that?

1

There are 1 best solutions below

2
math scat On

Yes there is an answer for it. Don't use exit() in tkinter exe. For that use sys.exit() the built-in module in python.

The full code:

from tkinter import *
from sys import exit as sexit

def exit_():
    window.destroy()
    sexit()

window = Tk()
text = Label(window, text = "Hello World")
text.pack()
window.protocol('WM_DELETE_WINDOW', exit_)
window.mainloop()