2016-02-24 37 views
0
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() 

我想回顾一下我在第二个定义def dynamic_data_entry中创建的bmi这个术语。我怎样才能做到这一点?参考python中的先前语句

另外,我怎么能把BMI给出的权重组转换成一个字符串,然后我可以把它放入由SQLite创建的数据库中?

+0

是凹处是否正确? 'dynamic_data_entry'是'bmicalculation'中的一个函数吗? –

+1

你不能引用一行代码。正确使用变量引用 –

回答

1

使bmi类变量像self.bmitotalindex也是如此。

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 

有了这些,现在你可以在你的dynamic_data_entry方法来访问self.bmiself.totalindex

0

如果你的第二个功能是def dynamic_data_entry里面功能def bmicalculation(self)bmi变量应该是在函数内部访问的反正。

如果不是的话,你需要把它作为参数传递给def dynamic_data_entry