import sqlite3
my_path="D:\\testing\\sqlite\\my_db.db" #Change the path
my_conn = sqlite3.connect(my_path)
We will use my_conn in our further script as the connection object to get our records.
# add one Label
l1 = tk.Label(my_w, text='Student ID: ',font=20 )
l1.grid(row=1,column=1,pady=5)
# add one text box
t1 = tk.Text(my_w, height=1, width=4,bg='yellow',font=22)
t1.grid(row=1,column=2,padx=5)
We need one more Label ( l2) to display the returned details of the student. This Label initially will display the string Output. my_str = tk.StringVar()
# add one Label
l2 = tk.Label(my_w, textvariable=my_str,font=20,fg='red' )
l2.grid(row=3,column=1,columnspan=3,pady=20)
my_str.set("Output")
We will add one Button b1 to pass user entered data to a function to get the record details.
b1 = tk.Button(my_w, text='Show Details', width=15,bg='lightgreen',font=22,
command=lambda: my_details(t1.get('1.0',END)))
b1.grid(row=1,column=3)
try:
val = int(id) # check input is integer or not
except:
my_str.set("Check input")
Inside the try block ( the input id is integer ) we will keep one more try and except. In this try block we will keep the database handling part and in except block we will display message Database error. The full code of the function my_details() is here.
def my_details(id):
try:
val = int(id) # check input is integer or not
try:
my_data=(val,)
q="SELECT * FROM student WHERE id= ?"
my_cursor=my_conn.execute(q,my_data)
data_row=my_cursor.fetchone()
my_str.set(data_row)
except sqlite3.Error as my_error:
print("error: ",my_error)
except:
my_str.set("Check input")
We used SQL with WHERE to collect details of the record by using student id from the student table.
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 *
####### end of connection ####
my_w = tk.Tk()
my_w.geometry("400x200")
# add one Label
l1 = tk.Label(my_w, text='Student ID: ',font=20 )
l1.grid(row=1,column=1,pady=5)
# add one text box
t1 = tk.Text(my_w, height=1, width=4,bg='yellow',font=22)
t1.grid(row=1,column=2,padx=5)
b1 = tk.Button(my_w, text='Show Details', width=15,bg='lightgreen',font=22,
command=lambda: my_details(t1.get('1.0',END)))
b1.grid(row=1,column=3)
my_str = tk.StringVar()
# add one Label
l2 = tk.Label(my_w, textvariable=my_str,font=20,fg='red' )
l2.grid(row=3,column=1,columnspan=3,pady=20)
my_str.set("Output")
def my_details(id):
try:
val = int(id) # check input is integer or not
try:
my_data=(val,)
q="SELECT * FROM student WHERE id= ?"
my_cursor=my_conn.execute(q,my_data)
data_row=my_cursor.fetchone()
my_str.set(data_row)
except sqlite3.Error as my_error:
print("error: ",my_error)
except:
my_str.set("Check input")
my_w.mainloop()
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
my_path="D:\\testing\\sqlite\\my_db.db" #Change the path
my_conn = create_engine("sqlite:///" + my_path)
###### end of connection ####
##### tkinter window ######
import tkinter as tk
from tkinter import *
####### end of connection ####
my_w = tk.Tk()
my_w.geometry("400x200")
# add one Label
l1 = tk.Label(my_w, text='Student ID: ',font=20 )
l1.grid(row=1,column=1,pady=5)
# add one text box
t1 = tk.Text(my_w, height=1, width=4,bg='yellow',font=22)
t1.grid(row=1,column=2,padx=5)
b1 = tk.Button(my_w, text='Show Details', width=15,bg='lightgreen',font=22,
command=lambda: my_details(t1.get('1.0',END)))
b1.grid(row=1,column=3)
my_str = tk.StringVar()
# add one Label
l2 = tk.Label(my_w, textvariable=my_str,font=20,fg='red' )
l2.grid(row=3,column=1,columnspan=3,pady=20)
my_str.set("Output")
def my_details(id):
try:
val = int(id) # check input is integer or not
try:
my_data=(val,)
q="SELECT * FROM student2 WHERE id= ?"
my_cursor=my_conn.execute(q,my_data)
data_row=my_cursor.fetchone()
my_str.set(data_row)
except SQLAlchemyError as e:
error=str(e.__dict__['orig'])
print(error)
except:
my_str.set("Check input")
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.