Displaying records from MySQL table using Treeview

Tkinter Treeview Displaying MySQL records in grid

MySQL: Records in Treeview
Connect to MySQL database display rows from MySQL.
We will display records from MySQL Student table using Treeview in Tkinter window.
First we will import ttk module.
from tkinter import ttk

Connecting and displaying MySQL table data in Tkinter window using Treeview insert with columns

Connect to MySQL database

from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
We will use my_conn in our further script as the connection object to get our records.

We will use Query with LIMIT to collect 10 records from the student table.

We used the returned MySQLCursor i.e my_conn as an iterator

We will use one for loop to collect each row of record from the data set. Each row of data ( here student ) is a tuple. So we used another for loop to display each element of this tuple.

Displaying records: Checking connection

Run this code without using Tkinter to check your MySQL record retrieval. If this code is working fine then you can focus on Tkinter part.
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
r_set=my_conn.execute('''SELECT * from student LIMIT 0,10''');
for dt in r_set: 

Integrating MySQL data to Treeview

In above code we have the record r_set which we can loop through and insert the rows to the Treeview. This is the line which does that.
for dt in r_set:
To one Tkinter window we will integrate the above code. The Treeview we created will have header and columns. After creating all these we will insert the rows of data using above code.

With Tkinter the Full code is here
from tkinter import ttk
import tkinter as tk
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")  

# Creating tkinter my_w
my_w = tk.Tk()
# Using treeview widget
trv = ttk.Treeview(my_w, selectmode ='browse')
# number of columns
trv["columns"] = ("1", "2", "3","4","5")
# Defining heading
trv['show'] = 'headings'
# width of columns and alignment 
trv.column("1", width = 30, anchor ='c')
trv.column("2", width = 80, anchor ='c')
trv.column("3", width = 80, anchor ='c')
trv.column("4", width = 80, anchor ='c')
trv.column("5", width = 80, anchor ='c')
# Headings  
# respective columns
trv.heading("1", text ="id")
trv.heading("2", text ="Name")
trv.heading("3", text ="Class")
trv.heading("4", text ="Mark")  
trv.heading("5", text ="Gender")

# getting data from MySQL student table 
r_set=my_conn.execute('''SELECT * from student LIMIT 0,10''')
for dt in r_set: 
    trv.insert("", 'end',iid=dt[0], text=dt[0],
               values =(dt[0],dt[1],dt[2],dt[3],dt[4]))
This will print 10 rows of records from student table.
Displaying MySQL records using Entry or Label
Treeview Treeview insert Pagination of Records of MySQL Delete Records Select -Edit-update MySQL Product table using Treeview
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer