StringVar requires a wizard:
>>> StringVar(Tk()) <Tkinter.StringVar instance at 0x0000000004435208> >>>
or more often:
>>> root = Tk() >>> StringVar() <Tkinter.StringVar instance at 0x0000000004435508>
When you create an instance of Tk, a new interpreter is created. Prior to this, nothing works:
>>> from Tkinter import * >>> StringVar() Traceback (most recent call last): File "<input>", line 1, in <module> File "C:\Python26\lib\lib-tk\Tkinter.py", line 251, in __init__ Variable.__init__(self, master, value, name) File "C:\Python26\lib\lib-tk\Tkinter.py", line 182, in __init__ self._tk = master.tk AttributeError: 'NoneType' object has no attribute 'tk' >>> root = Tk() >>> StringVar() <Tkinter.StringVar instance at 0x00000000044C4408>
The problem with the examples you found is that, probably, in the literature they show only partial fragments that should be inside the class or in a longer program, so import and other code are not explicitly specified.
source share