Tkinter and IDE's

Mike Clarkson support at internetdiscovery.com
Fri Nov 23 22:48:50 CET 2001


On Wed, 21 Nov 2001 02:46:15 GMT, "Don Arnold" <darnold02 at sprynet.com>
wrote:

>Here's the simple code I was trying to run (from 'Programming Python', 2nd
>ed):
Thanks for the explicit  example. The problem is in your code, not
with Idle.

>from Tkinter import *
This is unfortunately the cause of the problem in this case.
In Python, if you're having troubles, remove all the 'import *'
you can find and replace them with 'import' for safety.

>def quit():
>  print "Hello, I must be going"
>  root.quit()

The problem here is that root is not your root - it's a default
root in Tkinter that came in with your 'import *'. The is an evil
timesaving feature of Tkinter that I would like to see removed.
It is the first instance of a Tk(), which in this case happens to
be Idle's. So your quit() says 'quit Idle'.

>root = Tk()
This root is your root, which is a different root from quit().

>widget = Button(root, text="Hello, event world", command = quit)
>widget.pack()
>root.mainloop()

This is correct, but it should be followed by a root.destroy().
root.quit() quits the mainloop, but *does not destroy myroot*.

My revised version of your program is:

import Tkinter

global myroot
myroot = Tkinter.Tk()

def quit():
    print "Hello, I must be going"
    #  import sys
    #  sys.exit()
    myroot.quit()

widget = Tkinter.Button(myroot, text="Hello, event world",
	command = quit)
widget.pack()
myroot.mainloop()
myroot.destroy()

>When I run it in IDLE, the tk window pops fine. Clicking the button once
>outputs the message to the interactive window as expected, but doesn't
>shutdown the tk window.  Clicking the button a second time shuts down the tk
>window, and IDLE along with it. I just found that if I delete(root) in the
>interactive window before the 2nd click, the tk window closes and I raise a
>NameError exception on 'root', but at least IDLE stays up and running.

My revised version of your program does what you expect, and doesn't
kill Idle, because root.quit() isn't called.  Maybe you should send a
note to the author of Programming Python to say that using Tkinter's
default root is confusing, and perhaps the example could be rewritten.

>Sorry for being vague earlier.

Thank's for the explicit example. Actually there is still a problem
left here (the Tcl interpreter is not completely and cleanly destroyed
by myroot.destroy()), but that's another issue.

>Hope this helps,
It did - it helped me find a bug in my Tkinter debugging tools!
(http://tix.sourceforge.net/Tide in Tkinspect.py)

Mike.




More information about the Python-list mailing list