import sqlite3
my_conn = sqlite3.connect('my_db.db')
We will use my_conn in our further script as the connection object to get our records.
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/my_database")
We used Query with LIMIT to collect 10 records and displayed in our window.
def my_show():
for w in my_w.grid_slaves(): # remove all rows first
w.grid_forget() # remove row
r_set=my_conn.execute('''SELECT * from student LIMIT 0,10''');
i=0 # row value inside the loop
for student in r_set:
for j in range(len(student)):
e = Entry(my_w, width=10, fg='blue')
e.grid(row=i, column=j)
e.insert(END, student[j])
e = Button(my_w, text='X',command=lambda d=student[0] : my_delete(d))
e.grid(row=i, column=j+1)
i=i+1
This will print 10 rows of records from student table and each row will have one Delete button at the end. The click event of the button will pass the unique row id to the function my_delete().
e = Button(my_w, text='X',command=lambda d=student[0] : my_delete(d))
To understand how to create buttons dynamically and use the click event to pass value click here.
def my_delete(id):
my_var=msg.askyesnocancel("Delete ?","Delete id:"+str(id),icon='warning',default='no')
if my_var: # True if yes button is clicked
r_set=my_conn.execute("DELETE FROM student WHERE id=" + str(id) );
msg.showerror("Deleted ","No of records deleted = " + str(r_set.rowcount))
my_conn.commit()
my_show() # refresh the window with new records
Our function my_delete(id) receives value of id and display one message box with option asking user to confirm. User can select Yes, No or cancel. Our variable my_var will receive the user choice. One if condition is used to check the value of my_var and if it is True then delete query is executed.
# www.plus2net.com
# download updated script at https://www.plus2net.com/python/tkinter-sqlite-delete.php
import sqlite3
my_path="D:\\testing\\sqlite\\my_db.db" #Change the path
my_conn = sqlite3.connect(my_path)
###### end of connection ####
##### tkinter window ######
import tkinter as tk
from tkinter import *
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("400x350")
def my_show():
for w in my_w.grid_slaves(): # remove all rows first
w.grid_forget() # remove row
r_set=my_conn.execute('''SELECT * from student LIMIT 22,10''');
i=0 # row value inside the loop
for student in r_set:
for j in range(len(student)):
e = Entry(my_w, width=10, fg='blue')
e.grid(row=i, column=j)
e.insert(END, student[j])
e = Button(my_w, text='X',command=lambda d=student[0] : my_delete(d))
e.grid(row=i, column=j+1)
i=i+1
def my_delete(id):
my_var=msg.askyesnocancel("Delete ?","Delete id:"+str(id),icon='warning',default='no')
if my_var: # True if yes button is clicked
r_set=my_conn.execute("DELETE FROM student WHERE id=" + str(id) );
msg.showerror("Deleted ","No of records deleted = " + str(r_set.rowcount))
my_conn.commit()
my_show() # refresh the window with new records
my_show() # open the window with record at the starting
my_w.mainloop()
from sqlalchemy import create_engine
my_path="D:\\testing\\sqlite\\my_db.db" #Change the path
my_conn = create_engine("sqlite:///" + my_path)
import tkinter as tk
from tkinter import *
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("410x280")
def my_show():
for w in my_w.grid_slaves(): # remove all rows first
w.grid_forget() # remove rows
r_set=my_conn.execute('SELECT * FROM student LIMIT 25,10')
i=0
for student in r_set:
for j in range(len(student)):
e=tk.Label(my_w,width=10,fg='blue',text=student[j],
anchor='center')
e.grid(row=i,column=j)
e=Button(my_w,text='X',
command=lambda d=student[0],n=student[1]:my_delete(d,n))
e.grid(row=i,column=j+1)
i=i+1
def my_delete(id,name):
my_var=msg.askyesnocancel("Delete?",\
"Delete ID:" + str(id) + name, icon='warning',default='no')
if my_var:
r_set=my_conn.execute('DELETE FROM student WHERE id='+str(id))
msg.showerror("Deleted","No of records deleted: "+str(r_set.rowcount))
my_show()
my_show()
my_w.mainloop()
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.