def bmicalculation(self):
bmiheight=self.heightcm.get()
print(bmiheight)
bmiweight=self.weightkg.get()
bmi= float((bmiweight)/((bmiheight / 100)**2))
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'
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'
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'
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'
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)
def dynamic_data_entry(self):
#this is what adds the data to the database. Bmi has to be changed to the value of bmi and weightclass has to be change to the weightclass
timestamp = str(datetime.datetime.now().date())
bodymassindex = '20'
weightclass = str(totalindex)
c.execute("INSERT INTO BMIStorage (timestamp, bodymassindex, weightclass) VALUES (?, ?, ?)",(timestamp, bodymassindex, weightclass))
conn.commit()
我想在我的第二个定义bmi
中回顾我创建的def dynamic_data_entry
这个词。我怎么能这样做?
另外,我怎样才能将BMI给出的权重组转换为一个字符串,然后我可以将其放入由SQLite创建的数据库中?
答案 0 :(得分:1)
使bmi
类变量类似于self.bmi
。同样适用于totalindex
。
bmi= float((bmiweight)/((bmiheight / 100)**2))
self.bmi = bmi
.
.
.
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
现在,您可以使用self.bmi
方法访问self.totalindex
和dynamic_data_entry
。
答案 1 :(得分:0)
如果你的第二个函数def dynamic_data_entry
def bmicalculation(self)
函数,那么bmi
变量应该可以在函数内部访问。
如果不是,则需要将其作为参数传递给def dynamic_data_entry