tkinter questions: behavior of StringVar, etc

John Posner jjposner at
Sun Mar 29 23:20:37 CEST 2009

Scott David Daniels said:

 >> You ask, "What exactly is the role of ...", rather than saying
 >> something like, "I don't understand the role of ...", and continue
 >> to ask why the code is not architected the way you first expected
 >> it to be architected, calling those things you do not understand
 >> "magic"  (not "magically" which would at least invoke a sense of
 >> wonder, rather than indignation).

I agree with Scott that Alan could use a little attitude adjustment. OTOH,
the following IDLE transcript does suggest that some "magic" is occurring:

 >>> ================================ RESTART
 >>> from Tkinter import *
 >>> root = Tk()
 >>> ss = StringVar()
 >>> ================================ RESTART
 >>> from Tkinter import *
 >>> ss = StringVar()

 Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    ss = StringVar()
  File "C:\Python26\lib\lib-tk\", line 251, in __init__
    Variable.__init__(self, master, value, name)
  File "C:\Python26\lib\lib-tk\", line 182, in __init__
    self._tk =
 AttributeError: 'NoneType' object has no attribute 'tk'

Here's the "magic" in this situation, which is not exactly highlighted in
existing Tkinter documentation:

   If you do not specify a master object in the StringVar() statement, the
Tk object becomes
   the variable's master. If no Tk object exists, an error occurs.
   You can create a Tk object explicitly with the Tk() statement. A Tk
object is created
   implicitly when you create a widget -- for example, with Frame() or

I *did* find this in the "Configuration Interface" section of 
Fredrik Lundh's "An Introduction to Tkinter"

  widgetclass(master, option=value, ...)   ->  widget

      Create an instance of this widget class, as a child to the given 
      master, and using the given options. All options have default 
      values, so in the simplest case, you only have to specify the 
      master. You can even leave that out if you really want; Tkinter 
      then uses the most recently created root window as master.

BTW, the last sentence appears to be false in Python 2.6.1 -- the *first*
root window is used as the master.

E-mail message checked by Spyware Doctor (
Database version: 5.12060

More information about the Python-list mailing list