2016-02-24 57 views
1
class bmicalculator(): 
    #class created for the bmi calculator GUI and processing the numbers (pain in the ass to make)# 
    def __init__(self,master): 

      self.heightcm=DoubleVar() 
      self.weightkg=DoubleVar() 

      self.master=master 
      self.master.geometry('250x200+100+200') 
      self.master.title('BMI Calculator') 

      self.label2=Label(self.master,text='Welcome to the BMI Calculator',fg='red').grid(row=0,column=0) 
      self.label2=Label(self.master,text='Please enter your height in centimetres',fg='black').grid(row=3,column=0) 
      self.label2=Label(self.master,text='Please enter your weight in kilograms',fg='black').grid(row=4,column=0) 

      self.myheight=Entry(self.master,textvariable=self.heightcm).grid(row=3,column=1) 
      self.myweight=Entry(self.master,textvariable=self.weightkg).grid(row=4,column=1) 
      self.button4=Button(self.master,text="Calculate BMI",fg='red',command=self.bmicalculation).grid(row=7,column=0) 
      self.button5=Button(self.master,text="Exit",fg='red',command=self.exit).grid(row=9,column=0) 

    def bmicalculation(self): 
      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) 

    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 = self.bmi 
      weightclass = self.totalindex 
      c.execute("INSERT INTO BMIStorage (timestamp, bodymassindex, weightclass) VALUES (?, ?, ?)",(timestamp, bodymassindex, weightclass)) 
      conn.commit() 
      create_table() 

    def create_table(): 
      c.execute('CREATE TABLE IF NOT EXISTS BMIStorage(timestamp TEXT,bmi REAL,weightclass TEXT)') 

獲取dynamic_data_entry只需一個參數(給出0)的錯誤。我不知道如何解決或出了什麼問題。這是一個帶有GUI的BMI的代碼,我想在其中將用戶的輸入寫入數據庫以及日期。我無法將輸入中的數據寫入數據庫。僅顯示一個參數(0給出)

**功能是一類 ***更新,提高全班

+0

是您'fucntion'是'class'的方法之間的區別? – Arman

+0

是的,這個函數是一個類的方法 –

+0

你什麼時候調用這個方法? – RafaelC

回答

0

我想我已經找到你的錯誤的方法。

你是路過,將作爲命令Button類這樣的功能:

self.button6=Button(...., command=self.dynamic_data_entry).grid(row=8,column=0) 

但是,要傳遞的方法立竿見影,這將導致Error你得到,一旦方法本身需要一個隱含的參數,即self

self意味着你的班級的對象。但是,由於您直接通過方法,因此不涉及任何對象。

因此,你應該做這樣的代替:

obj = bmicalculator(master) 
self.button6=Button(...., command=obj.dynamic_data_entry).grid(row=8,column=0) 

這樣一來,你告訴Button類來調用對象的方法,並且在這種情況下,self論證會存在,因爲實例化對象存在。

確保瞭解command=self.dynamic_data_entrycommand=obj.dynamic_data_entry

相關問題