from tkinter import ttk
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.
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:
print(dt)
for dt in r_set:
trv.insert("",'end',iid=dt[0],values=(dt[0],dt[1],dt[2],dt[3],dt[4]))
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. from sqlalchemy import create_engine, text
my_conn = create_engine("mysql+mysqldb://id:pw@localhost/my_db")
my_conn = my_conn.connect()
## collect data from database to check
# r_set = my_conn.execute(text("SELECT * FROM student"))
# for row in r_set:
# print(row)
from tkinter import ttk # for Treeview
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x280") # width and height of window
my_w.title("www.plus2net.com") # title
# Using treeview widget
trv = ttk.Treeview(my_w, selectmode='browse')
trv.grid(row=1, column=1, padx=20, pady=20)
# 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(text('SELECT * from student LIMIT 0,10'))
# Adding rows of data to Treeview
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]))
my_w.mainloop() # Keep the window open.
This will print 10 rows of records from student table.
trv.tag_configure('pass', background='lightgreen')
trv.tag_configure('fail', background='yellow')
Adding to rows based on (one liner) if condition check
for dt in r_set:
my_tag='pass' if dt[3] >=75 else 'fail'
trv.insert("", 'end',iid=dt[0], text=dt[0],
values =(dt[0],dt[1],dt[2],dt[3],dt[4]), tags=(my_tag))
We can add more styles to the tag_configure().
trv.tag_configure('pass', background='lightgreen',font=('Times',12,'normal'))
trv.tag_configure('fail', background='yellow',font=('Times',12,'overstrike'))
trv.tag_configure('A', background='lightgreen')
trv.tag_configure('B', background='lightblue')
trv.tag_configure('C', background='lightyellow')
trv.tag_configure('D', background='white')
Based on the mark we can have different colour for the row
r_set=my_conn.execute('''SELECT * from student LIMIT 0,10''')
for dt in r_set:
if(dt[3]>=80): my_tag='A'
elif(dt[3]>=70): my_tag='B'
elif(dt[3]>=60): my_tag='C'
else: my_tag='D'
trv.insert("", 'end',iid=dt[0], text=dt[0],
values =(dt[0],dt[1],dt[2],dt[3],dt[4]), tags=(my_tag))
trv.tag_configure('gray', background='lightgray')
trv.tag_configure('normal', background='white')
my_tag='normal' # default value
# getting data from MySQL student table
r_set=my_conn.execute('''SELECT * from student LIMIT 0,10''')
for dt in r_set:
my_tag='gray' if my_tag=='normal' else 'normal'
trv.insert("", 'end',iid=dt[0], text=dt[0],
values =(dt[0],dt[1],dt[2],dt[3],dt[4]),tags=(my_tag))
style = ttk.Style(my_w) # set theam to clam
style.theme_use("clam")
style.configure("Treeview", background="black",
fieldbackground="black", foreground="white")
Managing style of Treeview
from tkinter import ttk
import tkinter as tk
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/db_name")
# Creating tkinter my_w
my_w = tk.Tk()
my_w.geometry("400x380") # width and height of the window
my_w.title("www.plus2net.com")
r_set=my_conn.execute("SELECT * from student LIMIT 0,5")
l1=[r for r in r_set.keys()] # List of column headers
# Using treeview widget
trv = ttk.Treeview(my_w, selectmode ='browse',columns=l1,
show='headings',height=10)
trv.grid(row=1,column=1,padx=20,pady=20)
# set columns and headings for Treeview
for i in l1:
trv.column(i, anchor ='c', width=70)
trv.heading(i, text =i)
# Adding rows of data from MySQL student table to treeview
for row in r_set:
trv.insert("", 'end',iid=row[0], text=row[0],
values =list(row))
my_w.mainloop()
from tkinter import ttk
import tkinter as tk
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/db_name")
# Creating tkinter my_w
my_w = tk.Tk()
my_w.geometry("420x300") # width and height of the window
my_w.title("www.plus2net.com")
r_set=my_conn.execute("SELECT * from student") # collect all records
l1=[r for r in r_set.keys()] # List of column headers
# Using treeview widget
trv = ttk.Treeview(my_w, selectmode ='browse',columns=l1,
show='headings',height=10)
trv.grid(row=1,column=1,padx=20,pady=20)
# set columns and headings for Treeview
for i in l1:
trv.column(i, anchor ='c', width=70)
trv.heading(i, text =i)
# Adding rows of data from MySQL student table to treeview
for row in r_set:
trv.insert("", 'end',iid=row[0], text=row[0],
values =list(row))
vs = ttk.Scrollbar(my_w,orient="vertical", command=trv.yview)#V Scrollbar
trv.configure(yscrollcommand=vs.set) # connect to Treeview
vs.grid(row=1,column=2,sticky='ns')
my_w.mainloop()
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.