This is NOT a duplicate of Python tkinter mainloop not quitting on closing the window I have an app that builds on tkinter. I observed at sometimes after I close the window using the X button, the code will not execute past the mainloop() line. This happens completely randomly about 10% of chance. Rest of the time, it works like a charm. I would like to ask if there are any way to force it. As I said, the code blocks on the mainloop() line, thus calling sys.exit() after it does not help. I am using Python 3.9.8. This is not 100% reproducible, but here is something that might trigger the problem: from tkinter import * root = Tk() Label(root, 'hi').pack() mainloop() print('exited') Continue reading...