MySQL Blob ( Single )image in Tkinter



Here we are displaying one image by using button and by using label. The image data is taken from Blob column of MySQL table.

Read more on how to display all records ( with image ) by looping

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_conn as 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()

Tkinter window to display MySQL Blob Data type image( All records)
Tkinter update Blob Data type with user uploaded image
Tkinter delete record with Blob Data type

Tkinter MySQL Record display MySQL Update MySQL Delete
Changes required for using SQLite database in place of MySQL
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    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-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer