Taking data from a database and putting into a table in a GUI
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 records
class:
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.