
command=lambda k=student[0]: del_data(k)def display(): # show all records
my_row=my_conn.execute("SELECT * FROM student_profile limit 0,4")
i=1 # data starts from row 1
global images
for student in my_row:
stream = io.BytesIO(student[2])
img=Image.open(stream)
img = ImageTk.PhotoImage(img)
e = Label(my_w, text=student[0])
e.grid(row=i,column=1,ipadx=20)
e = Label(my_w, text=student[1])
e.grid(row=i,column=2,ipadx=60)
e = Label(my_w, image=img)
e.grid(row=i, column=3,ipady=7)
e = Button(my_w,bg='Yellow',
text='X',command=lambda k=student[0]:del_data(k))
e.grid(row=i, column=4,ipady=7,ipadx=5)
images.append(img) # garbage collection
i=i+1
def del_data(s_id):
try:
my_var=msg.askyesnocancel("Delete Record",
"Are you sure ? ",icon='warning')
print(my_var)
if my_var:
query="DELETE FROM student_profile WHERE id=%s"
my_data=[s_id]
my_conn.execute(query,my_data)
print("Row Deleted ")
for row in my_w.grid_slaves():# remove widgets
row.grid_forget()
display() # refresh the list
except SQLAlchemyError as e:
error = str(e.__dict__['orig'])
print(error)
We defined my_conn as connection object
Collect the data from student_profile table by using SQLALchemy. # Deleting records with images
import tkinter as tk
from tkinter import *
from sqlalchemy import create_engine
import io
from PIL import Image, ImageTk
from sqlalchemy.exc import SQLAlchemyError
from tkinter import messagebox as msg
my_w = tk.Tk() # parent window
my_w.geometry("400x500") # size as width height
my_w.title("www.plus2net.com") # Adding a title
# database connection
my_conn = create_engine("mysql+mysqldb://root:test@localhost/my_tutorial")
# Column headers row 0
l1=Label(my_w, text='ID')
l1.grid(row=0,column=1)
l2=Label(my_w, text='Name')
l2.grid(row=0,column=2)
l3=Label(my_w, text='Photo')
l3.grid(row=0,column=3)
global img,images
images=[] # garbage collection
def display(): # show all records
my_row=my_conn.execute("SELECT * FROM student_profile limit 0,4")
i=1 # data starts from row 1
global images
for student in my_row:
stream = io.BytesIO(student[2])
img=Image.open(stream)
img = ImageTk.PhotoImage(img)
e = Label(my_w, text=student[0])
e.grid(row=i,column=1,ipadx=20)
e = Label(my_w, text=student[1])
e.grid(row=i,column=2,ipadx=60)
e = Label(my_w, image=img)
e.grid(row=i, column=3,ipady=7)
e = Button(my_w,bg='Yellow',
text='X',command=lambda k=student[0]:del_data(k))
e.grid(row=i, column=4,ipady=7,ipadx=5)
images.append(img) # garbage collection
i=i+1
display() # call to display all records
def del_data(s_id):
try:
my_var=msg.askyesnocancel("Delete Record",
"Are you sure ? ",icon='warning')
print(my_var)
if my_var:
query="DELETE FROM student_profile WHERE id=%s"
my_data=[s_id]
my_conn.execute(query,my_data)
print("Row Deleted ")
for row in my_w.grid_slaves():# remove widgets
row.grid_forget()
display() # refresh the list
except SQLAlchemyError as e:
error = str(e.__dict__['orig'])
print(error)
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.