murdering tkinter programs
Eric Brunel
eric_brunel at despammed.com
Wed Oct 13 04:32:48 EDT 2004
jeff wrote:
> At the start of a Tkinter program, you create a root window and then start
> the mainloop().
>
> So why isn't it standard practice to do the reverse when shutting down the
> program, via an exit command or quit button ?
> - that is, call .quit() first, then .destroy()
>
> All the tutorials and introductions only seem to call destroy() in their
> example programs.
Well, this is actually using a side-effect of the destroy method on the main
application window, which actually quits the main loop. But you are perfectly
right; one should do:
from Tkinter import *
root = Tk()
Button(root, text='Quit', command=root.quit).pack()
root.mainloop()
root.destroy()
So clicking on the button actually quits the main loop, but does not actually
destroy anything; you can see that by adding a time.sleep(2) after the
root.mainloop(). This is usually not considered as a problem, because Tkinter
programs rarely have anything left to do after the main loop is over. So the
next thing that happens is that the Python interpreter actually exits, and the
window disappear.
Note also that the quit method is available on all Tkinter widgets, and always
quits the main loop:
from Tkinter import *
root = Tk()
b = Button(root, text='Quit')
b['command'] = b.quit
b.pack()
root.mainloop()
root.destroy()
This can be quite handy if you're in a module that doesn't know anything about
the main window, but that can make the program quit anyway.
--
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
More information about the Python-list
mailing list