Tkinter question regarding StringVar() usage.

Christian Tanzer tanzer at swing.co.at
Sat Mar 11 04:10:19 EST 2000


Timothy Grant <tjg at avalongroup.net>

> I know I am missing something ridiculously simple here, but how come I
> cannot do the following...
> 
> >>> class test:
> ...     def __init__(self):
> ...             self.x = StringVar()
> ...
> >>> tjg = test()
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
>   File "<stdin>", line 3, in __init__
>   File "/usr/lib/python1.5/lib-tk/Tkinter.py", line 110, in __init__
>     Variable.__init__(self, master)
>   File "/usr/lib/python1.5/lib-tk/Tkinter.py", line 85, in __init__
>     self._tk = master.tk
> AttributeError: 'None' object has no attribute 'tk'
> 
> I am trying to create a class that has several StringVar instances as
> attributes, but it doesn't seem to work the way I'd like.
> 
> All help is greatly appreciated.

If you create a `Tk' object first, the error goes away:

-------------------------------------------------------------------------------
Python 1.5.2 (#5, Jan  4 2000, 11:37:02)  [GCC 2.7.2.1] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from Tkinter import *
>>> StringVar()
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python1.5/lib-tk/Tkinter.py", line 110, in __init__
    Variable.__init__(self, master)
  File "/usr/lib/python1.5/lib-tk/Tkinter.py", line 85, in __init__
    self._tk = master.tk
AttributeError: 'None' object has no attribute 'tk'
>>> root = Tk()
>>> StringVar()
<Tkinter.StringVar instance at 8270ed0>
>>>
-------------------------------------------------------------------------------

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list