Using StringVars in List of Dictionaries as tkInter variables for Scale and Label widgets

seanacais kccnospam at glenevin.com
Mon May 19 13:43:37 EDT 2008


I'm trying to build an unknown number of repeating gui elements
dynamically so I need to store the variables in a list of
dictionaries.  I understand that  Scale "variable" name needs to  be a
StringVar  but I cannot figure out how to initialize the dictionary.

I've tried the following code

ps = PowerSupply()		# Instantiate a Power Supply VM Object
numOPs = ps.getOnum()      # Find the number of outputs
OPValues = []			# Global list to hold one dictionary per output

for c in range(numOPs):
	OPValues.append({ })
	ps.initOPValues(c, OPValues[c])



ps.initOPValues is defined as follows:

	def initOPValues(self, OPname, dname):

		OPDefaults = {
			'Vval'  : 0,
			'Ival'  : 0,
			'Otemp' : 0
		}

		dname = dict((d,StringVar()) for d in OPDefaults)
		for d in OPDefaults:
				dname[d].set(OPDefaults[d])

I get the following error:

Traceback (most recent call last):
  File "C:\Code\gui\tkinter.py", line 165, in <module>
    ps.initOPValues(c, OPValues[c])
  File "C:\Code\gui\tkinter.py", line 47, i initOPValues
    dname = dict((d,StringVar()) for d in OPDefaults)
  File "C:\Code\gui\tkinter.py", line 47, in <genexpr>
    dname = dict((d,StringVar()) for d in OPDefaults)
  File "C:\Python25\lib\lib-tk\Tkinter.py", line 254, in __init__
    Variable.__init__(self, master, value, name)
  File "C:\Python25\lib\lib-tk\Tkinter.py", line 185, in __init__
    self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
Exception exceptions.AttributeError: "StringVar instance has no
attribute '_tk'"
 in <bound method StringVar.__del__ of <Tkinter.StringVar instance at
0x00B7D468>> ignored

Any help is appreciated!

Thanks,

Kevin



More information about the Python-list mailing list