2016-02-24 56 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