You’re setting your StringVar’s wrong…
textvariable = "self.var" + str(x)
This is just a string. Not a reference to the StringVar.
In your calc
function you’re doing this:
>>> float('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float:
Because your StringVar has no “value” — since the reference to the stringvar wasn’t passed correctly it’s not being updated, it’s by default set to the empty string.
If you wanted to iteratively get StringVar’s this way… There’s a few options:
Use __dict__
: self.__dict__['var%d' % i]
Use getattr
: getattr(self, 'var%d' % i)
Put them in a list / tuple and index either. Use a dict index that, although imo that’s redundant since using self.*
will make it be in the classes namespace… and you can just use the above.
Even correctly using your StringVar’s so that they update doesn’t guarantee float conversion… They could still be empty if the Entry is empty…
You’re also setting your command wrong. command=self.calc()
. This is calling the function and setting what it returns to be the command. Use command=self.calc
3
solved Cannot convert string to float in Python 3 [duplicate]