Tkinter question regarding StringVar() usage.

Fredrik Lundh effbot at telia.com
Sat Mar 11 04:56:02 EST 2000


Timothy Grant <tjg at avalongroup.net> wrote:
> 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.

you have to create a Tk root instance before
you can create variables.

(the widget constructors create root objects
if necessary, but variables and images don't.
good style is to always explicitly create the
root object before doing anything else)

</F>





More information about the Python-list mailing list