Reputation: 565
I'm making a BMI calculator just to get my head around tkinter. I am stuck on this one part- how do I make it so it gets a float number from the entry widget?
Here are some snippets of my code
Getting entry inputs (this is in a function called calculateBMI)
hgtval = float(height.get())
wgtval = float(weight.get())
Here is creating the variables and making the entry boxes.
hgt = StringVar()
wgt = StringVar()
height = Entry(root, stringvariable=hgt).grid(row=1, column=2)
weight = Entry(root, stringvariable=wgt).grid(row=2, column=2)
Running the whole program I am given this error:
C:\Python34\python.exe I:/programming/project/bmi.py
Traceback (most recent call last):
File "I:/programming/project/bmi.py", line 35, in <module>
height = Entry(root, stringvariable=hgt).grid(row=1, column=2)
File "C:\Python34\lib\tkinter\__init__.py", line 2478, in __init__
Widget.__init__(self, master, 'entry', cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 2086, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-stringvariable"
Upvotes: 2
Views: 3988
Reputation: 386362
The option is textvariable
, not stringvariable
.
Unrelated to this problem, when you do height = Entry(...).grid(...)
, height
will always be None
since that is what .grid(...)
returns. It is a best practice to separate widget creation from widget layout.
When you do that (separating creation from layout) you don't actually need to use StringVar
s, since you can get the value directly from the widget itself (eg: hgtval = float(height.get())
Upvotes: 4