Tkinter and IDE's

Fredrik Lundh fredrik at
Sat Nov 24 01:20:41 CET 2001

Mike Clarkson wrote:

> 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 *'.

What are you talking about?

It's perfectly safe to use from-import with Tkinter.

And there's no "root" variable in Tkinter:

    >>> from Tkinter import *
    >>> root
    Traceback (most recent call last):
      File "<pyshell#1>", line 1, in ?
    NameError: name 'root' is not defined

(if you think you see one, you've probably missed that IDLE doesn't
reset the __main__ namespace between runs.  the real default root
is stored in a variable called _default_root, and isn't imported by

And you don't seem to understand global variables either; even if
Tkinter did provide a root variable, it would have been overwritten
*before* the script created the button.  There's no way you can
end up in the quit function before that.

And the global statement doesn't do what you think it does.


All you've done is that you've made sure root.destroy is called when
the script returns from the call to mainloop  (this call isn't needed if
you run the script outside IDLE, or in a real IDE that doesn't insist on
running your program inside its own Python environment).

Do the same thing to the original script, and you get exactly the
same behaviour.


More information about the Python-list mailing list