Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21056

Re: Is it necessary to call Tk() when writing a GUI app with Tkinter?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Is it necessary to call Tk() when writing a GUI app with Tkinter?
Date 2012-02-29 19:17 -0500
References <27603449.17.1330492001624.JavaMail.geo-discussion-forums@vbbfv2> <6e1c521e-14be-4ec0-9ff0-7f23fd9cd3dc@f14g2000yqe.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.306.1330561055.3037.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-28 21:06 -0800
  Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-02-29 06:24 -0800
    Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Terry Reedy <tjreedy@udel.edu> - 2012-02-29 19:17 -0500
      Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-02-29 19:22 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Terry Reedy <tjreedy@udel.edu> - 2012-03-01 00:24 -0500
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-01 18:49 -0800
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-01 19:02 -0800
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Terry Reedy <tjreedy@udel.edu> - 2012-03-01 22:43 -0500
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Terry Reedy <tjreedy@udel.edu> - 2012-03-02 15:19 -0500
              Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Mel Wilson <mwilson@the-wire.com> - 2012-03-02 15:52 -0500
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? "Russell E. Owen" <rowen@uw.edu> - 2012-03-05 11:45 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 21:40 -0800
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-01 18:53 -0800
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Mark Roseman <mark@markroseman.com> - 2012-03-02 10:57 -0700
              Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-03-02 10:53 -0800
    Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 20:41 -0800
      Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Terry Reedy <tjreedy@udel.edu> - 2012-03-01 00:40 -0500
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 21:45 -0800
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 22:14 -0800
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-01 18:55 -0800
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 22:14 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 21:45 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 22:41 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 22:41 -0800
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-01 07:38 +0000
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 23:58 -0800
      Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-01 18:35 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-03-01 19:15 -0800
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-01 19:19 -0800
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-03-01 21:22 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-03-02 14:16 -0800
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-03 20:27 -0800

csiph-web