Tkinter question regarding StringVar() usage. (fwd)

Timothy Grant tjg at avalongroup.net
Mon Mar 13 20:43:42 EST 2000


Timothy Grant wrote:
> > >>> 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)

OK, now I'm really going to show my ignorance.

In general, I have been writing my code like this:

import mod1
import mod2

class def1:
	self.a = StringVar()
	self.b = StringVar()

class def2:
	self.x = StringVar()
	self.y = StringVar()

if __name__ == '__main__':
	root = Tk()
	Pmw.initialise(root)

	x = def1(root)

	root.mainloop()

I thought the above would would initialize my instance of Tk create an
instance of my base class and go.  However, it appears that Python is
generating its errors on parsing the class def1, which happens before I
actually initialize Tk and Pmw. What would be the correct way to
structure this thing?

Thanks.

-- 
Stand Fast,
    tjg.

Chief Technology Officer              tjg at exceptionalminds.com
Red Hat Certified Engineer            www.exceptionalminds.com
Avalon Technology Group, Inc.                   (503) 246-3630
>>>>>>>>>>>>Linux...Because rebooting isn't normal<<<<<<<<<<<<




More information about the Python-list mailing list