
command=lambda k=student[0]: del_data(k)def display():
my_cursor=my_conn.execute("SELECT * FROM student limit 0,10")
i=0
for student in my_cursor:
for j in range(len(student)):
e = Label(my_w,width=8,fg='blue', text=student[j],
relief='ridge', anchor="w")
e.grid(row=i, column=j)
# show the delete button
e = tk.Button(my_w,width=5,text='Del',fg='red',relief='ridge',
anchor="w",command=lambda k=student[0]:del_data(k))
e.grid(row=i, column=5)
i=i+1
def del_data(s_id): # delete record
try:
my_var=msg.askyesnocancel("Delete Record",
"Are you sure ? ",icon='warning')
print(my_var)
if my_var:
query="DELETE FROM student 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. import tkinter as tk
from tkinter import *
from tkinter import messagebox as msg
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
my_w = tk.Tk()
my_w.geometry("360x350")
my_conn = create_engine("mysql+mysqldb://root:test@localhost/my_tutorial")
def display():
my_cursor=my_conn.execute("SELECT * FROM student limit 0,10")
i=0
for student in my_cursor:
for j in range(len(student)):
e = Label(my_w,width=8,fg='blue', text=student[j],
relief='ridge', anchor="w")
e.grid(row=i, column=j)
# show the delete button
e = tk.Button(my_w,width=5,text='Del',fg='red',relief='ridge',
anchor="w",command=lambda k=student[0]:del_data(k))
e.grid(row=i, column=5)
i=i+1
display()
def del_data(s_id): # delete record
try:
my_var=msg.askyesnocancel("Delete Record",
"Are you sure ? ",icon='warning')
print(my_var)
if my_var:
query="DELETE FROM student 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.