
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 buttonb2=tk.Button(my_w,text=student[1])b2.grid(row=1,column=2)Managing image display. import iofrom 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) import tkinter as tk from tkinter import * import iofrom PIL import Image, ImageTkmy_w = tk.Tk()my_w.geometry("400x250") from sqlalchemy import create_enginefrom sqlalchemy.exc import SQLAlchemyErrormy_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 buttonb2=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()Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.