[英]python tkinter bmi calculator
Hi, Im having troubles getting strings to turn into floats. 嗨,我在让字符串变成浮点数方面遇到麻烦。 Im trying to get my entry texts to turn the input into a float so i can calculate the bmi.
我试图让我的输入文本将输入转换为浮点数,以便我可以计算出bmi。
from Tkinter import *
import tkMessageBox
class MyApp(object):
def __init__(self):
self.root = Tk()
self.root.wm_title("bmi!")
self.label = Label(self.root, text="Enter your height in box one and weight in box two",
font=('Helvetica', 20))
self.label.pack(padx=20,pady=10)
self.labeltext = StringVar()
self.labeltext.set("Another nice label!")
Label(self.root, textvariable=self.labeltext).pack()
self.entrytext = StringVar()
Entry(self.root, textvariable=self.entrytext).pack()
self.entrytext.trace('w', self.entry_changed)
self.entrytext2 = StringVar()
Entry(self.root, textvariable=self.entrytext2).pack()
self.entrytext2.trace('w', self.entry_changed)
self.root.mainloop()
def entry_changed(self, a, b, c):
s = self.entrytext.get()
b=self.entrytext2.get()
a=getdouble(s)
d=getdouble(b)
c=(a/(d**2))*703
self.labeltext.set(c)
MyApp()
I would try using float()
instead of getdouble()
as well as using str()
to convert the result back to a string. 我会尝试使用
float()
而不是getdouble()
以及使用str()
将结果转换回字符串。
You also need to handle ValueError
exceptions which occur when the text that entered is not a number. 您还需要处理
ValueError
异常,该异常在输入的文本不是数字时发生。
def entry_changed(self, a, b, c):
try:
mass = float(self.entrytext.get())
height = float(self.entrytext2.get())
except ValueError:
self.labeltext.set("--")
return
c = (mass/(height**2))*703
self.labeltext.set(str(c))
try 尝试
bmiheight=self.heightcm.get()
print(bmiheight)
bmiweight=self.weightkg.get()
bmi= float((bmiweight)/((bmiheight / 100)**2))
self.bmi = bmi
print(bmi)
self.label1=Label(self.master,text='Your BMI is %.2f' % bmi).grid(row=5,column=0)
if bmi <= 18.5:
self.label2=Label(self.master,text='This places you in the underweight group.',fg='blue').grid(row=6,column=0)
totalindex = 'underweight'
self.totalindex = totalindex
elif bmi >18.5 and bmi <25:
self.label3=Label(self.master,text='This places you in the healthy weight group.',fg='green').grid(row=6,column=0)
totalindex = 'healthy'
self.totalindex = totalindex
elif bmi >= 25 and bmi < 30:
self.label4=Label(self.master,text='This places you in the overweight group.',fg='orange').grid(row=6,column=0)
totalindex = 'overweight'
self.totalindex = totalindex
elif bmi >=30:
self.label5=Label(self.master,text='This places you in the obese group.',fg='red').grid(row=6,column=0)
totalindex = 'obese'
self.totalindex = totalindex
if bmi >0 and bmi <999999999999999999999:
self.button6=Button(self.master,text="Store Data",fg='red',command=self.dynamic_data_entry).grid(row=8,column=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.