A: 

Clean up right after mainloop() exits.

apalopohapa
+1  A: 

You can set up a binding that gets fired when the window is destroyed. Either bind to or add a protocol handler for WM_DELETE_WINDOW.

For example:

def callback():
    # your cleanup code here

...
root.protocol("WM_DELETE_WINDOW", callback)
Bryan Oakley