Record detail with image on user selection in Tkinter GUI


User selected  record with image

About MySQL table student_profile.

Our student_profile table has three columns,

id int(2)
student varchar(10)
profile_photo blob

This can be linked with our student table and photos of the student can be displayed.

Displaying student name for user selection

We defined my_conn as connection object Collect the data from student_profile table by using SQLALchemy.
All student names are displayed as options for the OptionMenu ( om1 ). If any option is selected then my_show() is triggered.
Tkinter OptionMenu with change option
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
my_conn = create_engine("mysql+mysqldb://root:test@localhost/my_tutorial")
try:
    my_row=my_conn.execute("SELECT student FROM student_profile")
    my_list = [r for r, in my_row]
except SQLAlchemyError as e:
  error=str(e.__dict__['orig'])
  print(error)    
options = tk.StringVar(my_w)
options.set(my_list[0]) # default value

om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=1,column=1,padx=50)

def my_show(*args):  # on select function 
    
    for i in my_list:
        if i==options.get():
            display_data(i)

Here is the code to display student id and name and photo
 def display_data(name):
    import io
    from PIL import Image, ImageTk
    query="SELECT id,student,profile_photo FROM student_profile WHERE student=%s"
    data=(name,)
    my_row=my_conn.execute(query,data)
    student = my_row.fetchone()
    
    img = Image.open(io.BytesIO(student[2]))
    img = ImageTk.PhotoImage(img)
    
    l1=tk.Label(my_w,text=student[0]) # student id
    l1.grid(row=1,column=2)
    l2=tk.Label(my_w,text=student[1]) # student name
    l2.grid(row=1,column=3)
    l3 = tk.Label(my_w,image=img )
    l3.grid(row=3,column=1)
Full code is here
import tkinter  as tk 
from tkinter import * 

#import cStringIO
my_w = tk.Tk()
my_w.geometry("400x250") 
my_w.title("www.plus2net.com")  # Adding a title
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
my_conn = create_engine("mysql+mysqldb://root:test@localhost/my_tutorial")
try:
    my_row=my_conn.execute("SELECT student FROM student_profile")
    my_list = [r for r, in my_row]
except SQLAlchemyError as e:
  error=str(e.__dict__['orig'])
  print(error)    
options = tk.StringVar(my_w)
options.set(my_list[0]) # default value

om1 =tk.OptionMenu(my_w, options, *my_list)
om1.grid(row=1,column=1,padx=50)

def my_show(*args):  # on select function 
    
    for i in my_list:
        if i==options.get():
            display_data(i)            
def display_data(name):
    import io
    from PIL import Image, ImageTk
    query="SELECT id,student,profile_photo FROM student_profile WHERE student=%s"
    data=(name,)
    my_row=my_conn.execute(query,data)
    student = my_row.fetchone()
    global img
    img = Image.open(io.BytesIO(student[2]))
    img = ImageTk.PhotoImage(img)
    
    l1=tk.Label(my_w,text=student[0])
    l1.grid(row=1,column=2)
    l2=tk.Label(my_w,text=student[1])
    l2.grid(row=1,column=3)
    l3 = tk.Label(my_w,image=img )
    l3.grid(row=3,column=1) 
    
options.trace('w',my_show)
my_w.mainloop()



Display all records with Photo Insert Image to Blob column

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