Tkinter GUI application to add, display update and delete Blob column data with image of MySQL table
Displaying Data from MySQL table
We defined my_connas connection object
Collect the data from student_profile table by using SQLALchemy.
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")
try:
my_row=my_conn.execute("SELECT id,student,profile_photo FROM student_profile WHERE id=1")
student = my_row.fetchone()
Here we used student to collect data. Here is the code to display student id and name.
# displaying student id on Button
b1=tk.Button(my_w,text=student[0])
b1.grid(row=1,column=1)
# displaying student name over button
b2=tk.Button(my_w,text=student[1])
b2.grid(row=1,column=2)
Managing image display.
import io
from PIL import Image, ImageTk
#collecting image and displaying
img = Image.open(io.BytesIO(student[2]))
img = ImageTk.PhotoImage(img)
b2 =tk.Button(my_w,image=img) # using Button
b2.grid(row=2,column=1)
l2 = tk.Label(my_w,image=img) # using Label
l2.grid(row=2,column=2)
Full code is here
import tkinter as tk
from tkinter import *
import io
from PIL import Image, ImageTk
my_w = tk.Tk()
my_w.geometry("400x250")
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")
try:
my_row=my_conn.execute("SELECT id,student,profile_photo FROM student_profile WHERE id=1")
student = my_row.fetchone()
except SQLAlchemyError as e:
error=str(e.__dict__['orig'])
print(error)
# displaying student id on Button
b1=tk.Button(my_w,text=student[0])
b1.grid(row=1,column=1)
# displaying student name over button
b2=tk.Button(my_w,text=student[1])
b2.grid(row=1,column=2)
#collecting image and displaying
img = Image.open(io.BytesIO(student[2]))
img = ImageTk.PhotoImage(img)
b2 =tk.Button(my_w,image=img) # using Button
b2.grid(row=2,column=1)
l2 = tk.Label(my_w,image=img) # using Label
l2.grid(row=2,column=2)
my_w.mainloop()