how can i display data in Tkinter treeview fetched from Mysql table using python
To resolve your problem, first you will need to read all the rows of the database using this query:
SELECT * FROM president
which you need to execute:
cursor.execute("""SELECT * FROM president""")
Now, simply loop over the rows and insert them one by one in tree
:
UPDATE:
I suppose your table structure is like this:
ID | name | votes | percentage
So you could run this:
cpt = 0 # Counter representing the ID of your code.for row in cursor: # I suppose the first column of your table is ID tree.insert('', 'end', text=str(cpt), values=(row[1], row[2], row[3])) cpt += 1 # increment the ID
iv used sqlite3 not MySQL but i am assuming that the value that is returned from the sql is put into a multidimensional array, these are array which require more than one index e.g
array[0][1]
the code below is for modifying the tree
for i in self.tree.get_children(): self.tree.delete(i) #clears current values from treefor student in StudentList: self.tree.insert("" , 0,values=(student[0],student[1]) #the index used would depend on what you want to be put into the tree #only uses one index per value instead of two as the for loop changes the first index
note that this was copied from my coursework(booking system) hence the names used