2

So, I'm currently making a program that serves as a BMI Calculator, where you can put in your weight and height and it will output it as a BMI and your weight group, with the option to record your results to a database where you can then go back and check your previous results. I'm having issues trying to get the information from a database and putting it in to the GUI for the user to see. Writing the data to the database is an issue I have already sold. I am using sqlite3 for my database and Tkinter for my GUI. How could I go about adding this function in to my code? Here is my current code

from Tkinter import *
import sqlite3
import time
import datetime

conn = sqlite3.connect('bmidatabase.db')
c = conn.cursor()

class Welcome():
#This is the class defining the first welcoming window. This window is used to navigate between previous weights and the calculator.
     def __init__(self,master):
        #This is the GUI for the starting Menu area. Features three buttons for navigating towards the BMI Calculator, the Records where we have to store variables entered and the exit# 

          self.master=master
          self.master.geometry('170x110+100+200')
          self.master.title('Welcome!')

          self.label1=Label(self.master,text='Welcome to the BMI Calculator',fg='red').grid(row=0,column=1)
          self.button1=Button(self.master,text="BMI Calculator",fg='green',command=self.gotobmicalculator).grid(row=1,column=1)
          self.button2=Button(self.master,text="Records",fg='blue',command=self.gotorecords).grid(row=2,column=1)
          self.button3=Button(self.master,text="Exit",fg='red',command=self.exit).grid(row=3,column=1)

     def exit(self):
        #Exit protocol for the exit button. This part is completely done.#
          self.master.destroy()

     def gotobmicalculator(self):
        #This is the BMI Calculator GUI#    
          root2=Toplevel(self.master)
          myGUI=bmicalculator(root2)

     def gotorecords(self):
        #This is where the previous records of BMI will be kept, hasn't been put in yet#
          root2=Toplevel(self.master)
          mygui=records(root2)

class bmicalculator():
     #class created for the bmi calculator GUI and processing the numbers (pain in the ass to make)#
     def __init__(self,master):

          c.execute('CREATE TABLE IF NOT EXISTS BMIStorage(timestamp TEXT,bodymassindex REAL,weightclass TEXT)') 

          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):
          global dynamic_data_entry
        #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()
          self.writetodatabase()

     def writetodatabase(self):
          for i in range(1):
               time.sleep(1)
          c.close()
          conn.close()

     def exit(self):
          #Exit protocol for the exit button. This part is completely done.#
          self.master.destroy()

class records():
     #class created to see records that have been previously inputted#
     def __init__(self,master):
          self.master=master
          self.master.geometry('250x200+100+200')
          self.master.title('Records')






def main():
     root=Tk()
     myGUIWelcome=Welcome(root)
     root.mainloop()

if __name__ == '__main__':
     main()

Any help with this would be much appreciated, I am very stuck at this point! Thanks, Ben

Benjamin Inverno
  • 41
  • 1
  • 2
  • 5
  • Where are you stuck? You have managed to write into the database, so you'll be able to modify that command to getting the values back. You have the tkinter running, so you are able to show values. What exaclty is the problem? – tfv Apr 13 '16 at 06:47
  • I don't know what command(s) to use to retrieve and what to use to get a table in to the GUI (i've tried using the print command and labels but that doesn't work) I'm new to both Tkinter and sqlite3 and don't know much in the way of commands, only what i've seen in tutorials – Benjamin Inverno Apr 13 '16 at 06:49
  • Have a look here: sqlite tutorial: http://zetcode.com/db/sqlitepythontutorial/ and here http://stackoverflow.com/questions/9348264/does-tkinter-have-a-table-widget – tfv Apr 13 '16 at 06:54

1 Answers1

2

Tkinter doesn't provide a table widget on its own, but you can use grid and insert Labels in it. To get all the data from your database you just need to execute a query SELECT * FROM <Table> and with .fetchall() you get a list with the duble. This is your recordsclass:

class records():
     #class created to see records that have been previously inputted#
    def __init__(self,master):
        self.master=master
        self.master.geometry('250x200+100+200')
        self.master.title('Records')
        self.connection = sqlite3.connect('bmidatabase.db')
        self.cur = self.connection.cursor()
        self.dateLabel = Label(self.master, text="Date", width=10)
        self.dateLabel.grid(row=0, column=0)
        self.BMILabel = Label(self.master, text="BMI", width=10)
        self.BMILabel.grid(row=0, column=1)
        self.stateLabel = Label(self.master, text="Status", width=10)
        self.stateLabel.grid(row=0, column=2)
        self.showallrecords()

    def showallrecords(self):
        data = self.readfromdatabase()
        for index, dat in enumerate(data):
            Label(self.master, text=dat[0]).grid(row=index+1, column=0)
            Label(self.master, text=dat[1]).grid(row=index+1, column=1)
            Label(self.master, text=dat[2]).grid(row=index+1, column=2)

    def readfromdatabase(self):
        self.cur.execute("SELECT * FROM BMIStorage")
        return self.cur.fetchall()

Since you've closed your connection to the database (which is a bug, because you cannot calculate another BMI), we need to open it again. After that we create a "header" with labels into the grid layout. Note here that we use grid on a seperate line. The reason for this is if we write self.ourLabel = Label().grid() we'll get a None back from the grid-method and can't use the reference for future purpose. So you will need to correct your other code. After that we fetch all data from the database and create 3 Labels for each row.

VRage
  • 1,458
  • 1
  • 15
  • 27