Horje
Student Results Management System Using Tkinter

In this tutorial, we will create a simple exam result management system using Tkinter for the GUI and SQLite for the database. We will start by creating two files in the project directory: a Python file for the code (mainprogram.py) and a database file (studentrecords.db). Let’s discuss the code for the system.

Student Results Management System Using Tkinter

Let’s see the steps to create a Student Results Management System using Tkinter:

Importing the libraries

Python
from tkinter import *
import sqlite3
import time

Creating the Database

We’ll create an SQLite database called studentrecords.db and a table named StudentData to store student information.

Python
con = sqlite3.connect('studentrecords.db')
c = con.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS StudentData(
                Name TEXT,
                Roll NUMBER,
                Gender TEXT,
                Maths NUMBER,
                Physics NUMBER,
                Chemistry NUMBER)""")
con.commit()
con.close()

Building the GUI

Now, let’s start building the GUI using Tkinter.

Python
root = Tk()
root.geometry('1000x500')
root.title('Exam Records')
var = IntVar()

# Creating Labels
Label(root, font=("Arial", 15), fg="purple", text="Exam Records").place(x=210, y=30)
Label(root, font=("Helvetica", 12), fg="purple", text="Name").place(x=69, y=120)
Label(root, font=("Helvetica", 12), fg="purple", text="Gender").place(x=69, y=164)
Label(root, font=("Helvetica", 12), fg="purple", text="Roll Number").place(x=69, y=208)
Label(root, font=("Helvetica", 12), fg="purple", text="Mathematics").place(x=69, y=250)
Label(root, font=("Helvetica", 12), fg="purple", text="Physics").place(x=69, y=290)
Label(root, font=("Helvetica", 12), text="Chemistry", fg="purple").place(x=69, y=330)

# Creating Entry Boxes
name = Entry(root, font=("Helvetica", 12), width=27, bg="lightblue")
rbutton1 = Radiobutton(root, font=("Helvetica", 12), fg="red", variable=var, value=1, text="Male")
rbutton2 = Radiobutton(root, font=("Helvetica", 12), fg="green", variable=var, value=2, text="Female")
rollno = Entry(root, font=("Helvetica", 12), width=27, bg="lightblue")
maths = Entry(root, font=("Helvetica", 12), width=27, bg="lightblue")
physics = Entry(root, font=("Helvetica", 12), width=27, bg="lightblue")
chemistry = Entry(root, font=("Helvetica", 12), width=27, bg="lightblue")

# Placing Widgets
name.place(x=170, y=122)
rbutton1.place(x=170, y=164)
rbutton2.place(x=250, y=164)
rollno.place(x=170, y=207)
maths.place(x=170, y=249)
physics.place(x=170, y=289)
chemistry.place(x=170, y=329)

# Create Buttons
Button(root, font=("Arial", 15), fg="white", bg="purple", text="Submit", borderwidth=0, command=lambda: clicksubmit()).place(x=242, y=369)
Button(root, font=("Helvetica", 12), bg="green", fg="white", text="Delete A Record", borderwidth=0, command=recdelete).place(x=220, y=410)
Button(root, font=("Helvetica", 12), text="Clear Database", bg="red", fg="white", borderwidth=0, command=clearall).place(x=223, y=450)

Adding Student Records

We will create a function clicksubmit() to handle the submission of new student records.

Python
def clicksubmit():
    con = sqlite3.connect('studentrecords.db')
    c = con.cursor()

    studentname = name.get()
    rollnum = rollno.get()
    math_marks = maths.get()
    phy_marks = physics.get()
    chem_marks = chemistry.get()
    gender = "Male" if var.get() == 1 else "Female"

    c.execute("""INSERT INTO StudentData(Name, Roll, Gender, Maths, Physics, Chemistry) VALUES(?,?,?,?,?,?)""",
              (studentname, rollnum, gender, math_marks, phy_marks, chem_marks))

    con.commit()
    con.close()

    name.delete(0, END)
    rollno.delete(0, END)
    maths.delete(0, END)
    physics.delete(0, END)
    chemistry.delete(0, END)

Deleting a Record

Next, we’ll create functions to delete a specific student record or clear the entire database.

Python
def clickok():
    con = sqlite3.connect('studentrecords.db')
    c = con.cursor()
    num = int(e.get())
    try:
        c.execute(f'DELETE FROM StudentData WHERE Roll={num}')
        con.commit()
        Label(deletewin, fg="red", text="Deleted Successfully", font=("Helvetica", 10)).place(x=6, y=115)
    finally:
        deletewin.after(3000, lambda: deletewin.destroy())

def recdelete():
    global deletewin
    deletewin = Toplevel(root)
    global e
    e = Entry(deletewin, font=("Helvetica", 12))
    e.place(x=5, y=40)
    Label(deletewin, font=("Helvetica", 12), text="Please Enter Roll No",
          fg="purple").place(x=5, y=10)
    Button(deletewin, text="OK", command=lambda: clickok(), font=("Helvetica", 10), 
           bg="lightpink", fg="purple", borderwidth=0).place(x=100, y=150)

def yesclk():
    con = sqlite3.connect('studentrecords.db')
    c = con.cursor()
    c.execute("DELETE FROM StudentData")
    con.commit()
    con.close()
    newwind.destroy()

def clearall():
    global newwind
    newwind = Toplevel(root)
    newwind.geometry('200x200')
    Button(newwind, font=("Helvetica", 12), text="Yes", borderwidth=0, bg="lightpink", 
           fg="purple", command=yesclk).place(x=7, y=160)
    Button(newwind, font=("Helvetica", 12), text="No", borderwidth=0, bg="lightpink", 
           fg="purple", command=lambda: newwind.destroy()).place(x=160, y=160)
    Label(newwind, font=("Arial", 15), text="Are You Sure?", fg="purple").place(x=15, y=25)

Viewing Student Records

Finally, let’s add functionality to display student marks sorted by subject.

Python
def display_results(subject):
    newwind = Toplevel(root)
    Label(newwind, text="Name", font=("Helvetica", 10), fg="purple", bg="lightpink").place(x=2, y=0)
    Label(newwind, text="Roll Number", font=("Helvetica", 10), fg="purple", bg="lightpink").place(x=152, y=0)
    Label(newwind, text="Marks Obtained", font=("Helvetica", 10), fg="purple", bg="lightpink").place(x=302, y=0)
    newwind.geometry('400x400')

    con = sqlite3.connect('studentrecords.db')
    c = con.cursor()
    rows = c.execute(f"SELECT Name, Roll, {subject} FROM StudentData ORDER BY {subject} desc")

    y1 = 30
    for row in rows:
        s_name, roll, marks = row
        Label(newwind, font=("Helvetica", 10), fg="purple", bg="lightpink", text=s_name).place(x=2, y=y1)
        Label(newwind, font=("Helvetica", 10), fg="purple", bg="lightpink", text=str(roll)).place(x=152, y=y1)
        Label(newwind, font=("Helvetica", 10), fg="purple", bg="lightpink", text=str(marks)).place(x=302, y=y1)
        y1 += 30

Button(root, text="Display Maths Results", bg="purple", fg="white", font=("Helvetica", 15), 
       command=lambda: display_results("Maths"), borderwidth=0).place(x=500, y=50)
Button(root, text="Display Physics Results", bg="purple", fg="white", font=("Helvetica", 15),
       command=lambda: display_results("Physics"), borderwidth=0).place(x=500, y=95)
Button(root, text="Display Chemistry Results", bg="purple", fg="white", font=("Helvetica", 15), 
       command=lambda: display_results("Chemistry"), borderwidth=0).place(x=500, y=140)

Running the Application

Finally, we need to run the Tkinter main loop to display our GUI.

Python
root.mainloop()

This is the complete code for the system

Complete Code

Python
from tkinter import *
import sqlite3
import time

def clickok():
    con=sqlite3.connect('studentrecords.db')
    c=con.cursor()
    num=e.get()
    num=int(num)
    print(num)
    try:
        c.execute(f'DELETE FROM StudentData WHERE Roll={num}')
        con.commit()
        button.config(command=lambda:button.pack_forget())
        l2=Label(deletewin,fg="red",text="Deleted Successfully",font=("Helvetica",10)).place(x=6,y=115)


        # l2=Label(deletewin,fg="red",text="No Such Record Found",font=("Helvetica",10)).place(x=6,y=115)        

    finally:
        deletewin.after(3000,lambda:deletewin.destroy())       
def recdelete():
    global deletewin
    deletewin=Toplevel(root)
    global e
    e=Entry(deletewin,font=("Helvetica",12))
    e.place(x=5,y=40)
    l=Label(deletewin,font=("Helvetica",12),text="Please Enter Roll No",fg="purple").place(x=5,y=10)
    global button
    button=Button(deletewin,text="OK",command=lambda:clickok(),font=("Helvetica",10),
                  bg="lightpink",fg="purple",borderwidth=0).place(x=100,y=150)
    
def yesclk():
    con=sqlite3.connect('studentrecords.db')
    c=con.cursor()
    c.execute("DELETE FROM StudentData")
    newwind.destroy()
    con.commit()
    con.close()
def clearall():
    global newwind
    newwind=Toplevel(root)
    newwind.geometry('200x200')
    yesbtn=Button(newwind,font=("Helvetica",12),text="Yes",borderwidth=0,bg="lightpink",
                  fg="purple",command=yesclk).place(x=7,y=160)
    nobtn=Button(newwind,font=("Helvetica",12),text="No",borderwidth=0,bg="lightpink",
                 fg="purple",command=lambda:newwind.destroy()).place(x=160,y=160)
    label1=Label(newwind,font=("Arial",15),text="Are You Sure?",fg="purple").place(x=15,y=25)

def clicksubmit():
    print("Called Function")
    con=sqlite3.connect('studentrecords.db')
    c=con.cursor()

    studentname=name.get()
    rollnum=rollno.get()
    math_marks=maths.get()
    phy_marks=physics.get()
    chem_marks=chemistry.get()
    gender=""
    gen=var.get()
    if(gen==1):
        gender+="Male"
    else:
        gender+="Female"    
    c.execute("""INSERT INTO StudentData(Name,Roll,Gender,Maths,Physics,Chemistry) VALUES(?,?,?,?,?,?)""",
              (studentname,rollnum,gender,math_marks,phy_marks,chem_marks))
    rows=c.execute("SELECT * from StudentData")
    print(type(rows))
    for row in rows:
        print(type(row))
        print(row)
        # print("Unable To Enter")

    con.commit()
    name.delete(0,END)
    rollno.delete(0,END)
    maths.delete(0,END)
    physics.delete(0,END)
    chemistry.delete(0,END)
root=Tk()
root.geometry('1000x500')
root.iconbitmap('ER.ico')
root.title('Exam Records')
var=IntVar()
global con
con=sqlite3.connect('studentrecords.db')
c=con.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS StudentData(Name TEXT,Roll NUMBER,Gender TEXT,\
                                Maths NUMBER,Physics NUMBER,Chemistry NUMBER)""")
# Creating Labels
header=Label(root,font=("Arial",15),fg="purple",text="Exam Records").place(x=210,y=30)
name_label=Label(root,font=("Helvetica",12),fg="purple",text="Name").place(x=69,y=120)
gender=Label(root,font=("Helvetica",12),fg="purple",text="Gender").place(x=69,y=164)
roll=Label(root,font=("Helvetica",12),fg="purple",text="Roll Number").place(x=69,y=208)
math_sub=Label(root,font=("Helvetica",12),fg="purple",text="Mathematics").place(x=69,y=250)
phy_sub=Label(root,font=("Helvetica",12),fg="purple",text="Physics").place(x=69,y=290)
chem_sub=Label(root,font=("Helvetica",12),text="Chemistry",fg="purple").place(x=69,y=330)

# Creating Entry Boxes
global name,rbutton1,rbutton2,rollno,maths,physics,chemistry
name=Entry(root,font=("Helvetica",12),width=27,bg="lightblue")
rbutton1=Radiobutton(root,font=("Helvetica",12),fg="red",variable=var,value=1,text="Male")
rbutton2=Radiobutton(root,font=("Helvetica",12),fg="green",variable=var,value=2,text="Female")
rollno=Entry(root,font=("Helvetica",12),width=27,bg="lightblue")
maths=Entry(root,font=("Helvetica",12),width=27,bg="lightblue")
physics=Entry(root,font=("Helvetica",12),width=27,bg="lightblue")
chemistry=Entry(root,font=("Helvetica",12),width=27,bg="lightblue")
# Placing Widgets
name.place(x=170,y=122)
rbutton1.place(x=170,y=164)
rbutton2.place(x=250,y=164)
rollno.place(x=170,y=207)
maths.place(x=170,y=249)
physics.place(x=170,y=289)
chemistry.place(x=170,y=329)
# Create a Submit Button , Delete Record , Clear Data Base
submit=Button(root,font=("Arial",15),fg="white",bg="purple",text="Submit",borderwidth=0,
              command=lambda:clicksubmit()).place(x=242,y=369)
con.commit()
con.close()
delete=Button(root,font=("Helvetica",12),bg="green",fg="white",text="Delete A Record",
              borderwidth=0,command=recdelete).place(x=220,y=410)
frame=Frame(root,bg="lightpink",width=500,height=500).place(x=500,y=0)
clearEntry=Button(root,font=("Helvetica",12),text="Clear Database",bg="red",fg="white",
                  borderwidth=0,command=clearall).place(x=223,y=450)
# Frame Buttons
def mathres():

    print("Inside Math")
    newwind=Toplevel(root)
    lab_header1=Label(newwind,text="Name",font=("Helvetica",10),fg="purple",bg="lightpink").place(x=2,y=0)
    lab_header2=Label(newwind,text="Roll Number",font=("Helvetica",10),fg="purple",bg="lightpink").place(x=152,y=0)
    lab_header3=Label(newwind,text="Marks Obtained",font=("Helvetica",10),fg="purple",bg="lightpink").place(x=302,y=0)
    newwind.geometry('400x400')
    con=sqlite3.connect('studentrecords.db')
    c=con.cursor()
    rows=c.execute("SELECT Name,Roll,Maths FROM StudentData ORDER BY Maths desc")
    x1=2 
    y1=30
    for row in rows:
        s_name=row[0]
        roll=row[1]
        math=row[2]
        lab1=Label(newwind,font=("Helvetica",10),fg="purple",bg="lightpink",
                   highlightcolor="purple",text=s_name).place(x=x1,y=y1)
        lab2=Label(newwind,font=("Helvetica",10),fg="purple",bg="lightpink",
                   highlightcolor="purple",text=str(roll)).place(x=x1+150,y=y1)
        lab3=Label(newwind,font=("Helvetica",10),fg="purple",bg="lightpink",
                   highlightcolor="purple",text=str(math)).place(x=x1+300,y=y1)
        y1+=30

def chemres():
    print("Inside Chem")
    newwind=Toplevel(root)
    lab_header1=Label(newwind,text="Name",font=("Helvetica",10),fg="purple",
                      bg="lightpink").place(x=2,y=0)
    lab_header2=Label(newwind,text="Roll Number",font=("Helvetica",10),fg="purple",
                      bg="lightpink").place(x=152,y=0)
    lab_header3=Label(newwind,text="Marks Obtained",font=("Helvetica",10),fg="purple",
                      bg="lightpink").place(x=302,y=0)
    newwind.geometry('400x400')
    con=sqlite3.connect('studentrecords.db')
    c=con.cursor()
    rows=c.execute("SELECT Name,Roll,Chemistry FROM StudentData ORDER BY Chemistry desc")
    x1=2 
    y1=30
    for row in rows:
        s_name=row[0]
        roll=row[1]
        math=row[2]
        lab1=Label(newwind,font=("Helvetica",10),fg="purple",bg="lightpink",
                   highlightcolor="purple",text=s_name).place(x=x1,y=y1)
        lab2=Label(newwind,font=("Helvetica",10),fg="purple",bg="lightpink",
                   highlightcolor="purple",text=str(roll)).place(x=x1+150,y=y1)
        lab3=Label(newwind,font=("Helvetica",10),fg="purple",bg="lightpink",
                   highlightcolor="purple",text=str(math)).place(x=x1+300,y=y1)
        y1+=30
 
def phyres():
    print("Inside Physics")
    newwind=Toplevel(root)
    lab_header1=Label(newwind,text="Name",font=("Helvetica",10),
                      fg="purple",bg="lightpink").place(x=2,y=0)
    lab_header2=Label(newwind,text="Roll Number",font=("Helvetica",10),
                      fg="purple",bg="lightpink").place(x=152,y=0)
    lab_header3=Label(newwind,text="Marks Obtained",font=("Helvetica",10),
                      fg="purple",bg="lightpink").place(x=302,y=0)
    newwind.geometry('400x400')
    con=sqlite3.connect('studentrecords.db')
    c=con.cursor()
    rows=c.execute("SELECT Name,Roll,Physics FROM StudentData ORDER BY Physics desc")
    x1=2 
    y1=30
    for row in rows:
        s_name=row[0]
        roll=row[1]
        math=row[2]
        lab1=Label(newwind,font=("Helvetica",10),fg="purple",bg="lightpink",
                   highlightcolor="purple",text=s_name).place(x=x1,y=y1)
        lab2=Label(newwind,font=("Helvetica",10),fg="purple",bg="lightpink",
                   highlightcolor="purple",text=str(roll)).place(x=x1+150,y=y1)
        lab3=Label(newwind,font=("Helvetica",10),fg="purple",bg="lightpink",
                   highlightcolor="purple",text=str(math)).place(x=x1+300,y=y1)
        y1+=30

button1=Button(frame,text="Display Maths Results",bg="purple",fg="white",font=("Helvetica",15),
               command=lambda:mathres(),borderwidth=0).place(x=500,y=50)
button2=Button(frame,text="Display Physics Results",bg="purple",fg="white",font=("Helvetica",15),
               command=phyres,borderwidth=0).place(x=500,y=95)
button3=Button(frame,text="Display Chemistry Results",bg="purple",fg="white",font=("Helvetica",15),
               borderwidth=0,command=chemres).place(x=500,y=140)

root.mainloop()

Output

Optionally we may add an icon for the application (.ico file) . Below is a screenshot of the home screen of the application.

temp1

Conclusion

In this article, we discussed about building a simple exam result management system using Tkinter for the GUI and SQLite for the database. This system allows you to add student records, delete specific records, clear the entire database, and display student marks sorted by subject. Further functionalities including adding graphs , exporting data to a file etc. can be added to enhance the application.




Reffered: https://www.geeksforgeeks.org


Python

Related
Replicate results from numpy.random in Dask Replicate results from numpy.random in Dask
Keep Decimal places in branca Colormap using Folium Keep Decimal places in branca Colormap using Folium
Extracting text from HTML file using Python Extracting text from HTML file using Python
How to suppress scientific notation when printing float values? How to suppress scientific notation when printing float values?
How can we display an image in a child process in Python How can we display an image in a child process in Python

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
18