[Tkinter-discuss] Debugging non-zero exit code

Michael Lange klappnase at web.de
Sat Feb 6 14:02:43 EST 2016


On Sat, 6 Feb 2016 18:43:07 +0200
Reinis Danne <rei4dan at gmail.com> wrote:

> Adding root.wm_protocol('WM_DELETE_WINDOW', root.quit) worked
> indeed, but only for the close button. It didn't work if I closed
> the window with Alt+F4 or File->Exit. On further inspection it
> already used WM_DELETE_WINDOW protocol in
> main.py::BKChem.initialize() like this:

That's odd, maybe the binding for Alt+F4 was overridden somewhere else
in the code? And doesn't File->Exit call the same _quit() procedure as
the one one linked to WM_DELETE_WINDOW?

> Calling destroy() at exit time feels like dealing with the issue
> with brute force instead of properly cleaning up when
> deactivating the widget.

destroy() itself does not end the application, only the Tk part.
This simple example may help to illustrate the difference between quit()
and destroy():

from Tkinter import *

def clean_up():
    print('Cleaning up.')
    return 0
root = Tk()
root.wm_protocol('WM_DELETE_WINDOW', root.quit)
l = Label(root, text='foo')
l.pack(padx=100, pady=100)
exitcode = clean_up()

As you can see, after quit() is called, the Label widget can still be
accessed, which fails (and causes the app to crash) after destroy() was
called. You can also very well perform some clean up routine after
destroy() has been called, as long as it does not require Tk to still be
As far as sys.exit() is concerned, I think it should not be necessary to
use it unless you want to control your app's exit code. Using sys.exit()
to end the app rather seems to be "brute force" than destroy(). I have no
idea though why the window won't close without it. Does your app use
threads? Improper use of threads at least might explain all sorts of odd

Best regards


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

	"There's only one kind of woman ..."
	"Or man, for that matter.  You either believe in yourself or you
		-- Kirk and Harry Mudd, "Mudd's Women", stardate 1330.1

More information about the Tkinter-discuss mailing list