0

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?

math scat
  • 310
  • 8
  • 17

1 Answers1

0

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()
math scat
  • 310
  • 8
  • 17
  • `sys.exit` will raise Exception `SystemExit`.Why don't use `exit()`? – jizhihaoSAMA Jun 27 '20 at 11:49
  • Exit() and quit() are not recommended for production modules (.exe) due to dependencies. https://www.geeksforgeeks.org/python-exit-commands-quit-exit-sys-exit-and-os-_exit/ – tan_an Jun 27 '20 at 12:43