On 2/29/2012 9:24 AM, Rick Johnson wrote:
On Feb 28, 11:06 pm, John Salerno<johnj...@gmail.com> wrote:
However, in the Python documentation, I see this:
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()
I tried the above and I got the following error:
Traceback (most recent call last):
File "C:\Users\John\Desktop\gui.py", line 12, in<module>
root.destroy()
File "C:\Python32\lib\tkinter\__init__.py", line 1714, in destroy
self.tk.call('destroy', self._w)
_tkinter.TclError: can't invoke "destroy" command: application has been
destroyed
So apparently closing the window with the X button (on Windows)
>> implicitly calls the destroy() method of the root frame.
>> If that's the case, why does the documentation explicitly call it?
I do not know if tk has changed since the example was written or if it
was buggy from the beginning. I opened an issue to fix it.
http://bugs.python.org/issue14163
Most applications will have both: user destroying, and program
destroying.
from tkMessageBox import askyesnocancel
from tkinter.messagebox in 3.x
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title('Close Me -->')
self.protocol("WM_DELETE_WINDOW", self.onDestroyWindow)
def onDestroyWindow(self):
title = 'Confirm App Exit'
msg = 'Save changes before exiting?'
result = askyesnocancel(title, msg, default='cancel')
if result is None:
return
elif result is True:
print 'saving changes'
elif result is False:
print 'dont save changes'
self.destroy()
if __name__ == '__main__':
app = App()
app.mainloop()
This works as adjusted for 3.x. I presume that a quit button or menu
entry should also call onDestroyWindow so the effect is the same as
clicking the outer [X] button.
I tried the same approach to fix the doc example, but unlike your class
App(Tk), class App(Frame) does not a .protocol attribute. See the
tracker issue for all my comments on the example.
I considered removing both the quit button and 'root.destroy' to get a
beginning example that works properly, but as you said, having both is
common so I would like both if the solution is not too esoteric.
--
Terry Jan Reedy
--
http://mail.python.org/mailman/listinfo/python-list