0% found this document useful (0 votes)
20 views5 pages

Aj

This document contains code for a student management system GUI created with Tkinter. The code defines functions for adding, removing, and viewing student records stored in an SQLite database table. It also includes code to display the records in a treeview widget and reset or clear the form fields.

Uploaded by

Aniket
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

Aj

This document contains code for a student management system GUI created with Tkinter. The code defines functions for adding, removing, and viewing student records stored in an SQLite database table. It also includes code to display the records in a treeview widget and reset or clear the form fields.

Uploaded by

Aniket
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

import datetime

from tkinter import *


import tkinter.messagebox as mb
from tkinter import ttk
from tkcalendar import DateEntry # pip install tkcalendar
import sqlite3

#label font
headlabelfont = ("Noto Sans CJK TC", 15, 'bold')
labelfont = ('Garamond', 10,'bold')
entryfont = ('Garamond', 12)
#----------------------
# database connection
connector = sqlite3.connect('SchoolManagement.db')
cursor = connector.cursor()

connector.execute(
"CREATE TABLE IF NOT EXISTS SCHOOL_MANAGEMENT (STUDENT_ID INTEGER PRIMARY KEY
AUTOINCREMENT NOT NULL, NAME TEXT, EMAIL TEXT, PHONE_NO TEXT, GENDER TEXT, DOB
TEXT, STREAM TEXT)"
)

# all the functions

def reset_fields():
global name_strvar, email_strvar, contact_strvar, gender_strvar, dob,
Branch_strvar

for i in ['name_strvar', 'email_strvar', 'contact_strvar', 'gender_strvar',


'Branch_strvar']:
exec(f"{i}.set('')")
dob.set_date(datetime.datetime.now().date())

def reset_form():
global tree
tree.delete(*tree.get_children())

reset_fields()

def display_records():
tree.delete(*tree.get_children())

curr = connector.execute('SELECT * FROM SCHOOL_MANAGEMENT')


data = curr.fetchall()

for records in data:


tree.insert('', END, values=records)

def add_record():
global name_strvar, email_strvar, contact_strvar, gender_strvar, dob,
Branch_strvar

name = name_strvar.get()
email = email_strvar.get()
contact = contact_strvar.get()
gender = gender_strvar.get()
DOB = dob.get_date()
Branch = Branch_strvar.get()

if not name or not email or not contact or not gender or not DOB or not Branch:
mb.showerror('Error!', "Please fill all the missing fields!!")
else:
if not contact.isdigit():
mb.showerror('wrong type','this contact field only contain number')
try:
connector.execute(
'INSERT INTO SCHOOL_MANAGEMENT (NAME, EMAIL, PHONE_NO, GENDER, DOB,
Branch) VALUES (?,?,?,?,?,?)', (name, email, contact, gender, DOB, Branch)
)
connector.commit()
mb.showinfo('Record added', f"Record of {name} was successfully added")
reset_fields()
display_records()
except:
mb.showerror('Wrong type', 'This is database error')

def remove_record():
if not tree.selection():
mb.showerror('Error!', 'Please select an item from the database')
else:
current_item = tree.focus()
values = tree.item(current_item)
selection = values["values"]

tree.delete(current_item)

connector.execute('DELETE FROM SCHOOL_MANAGEMENT WHERE STUDENT_ID=%d' %


selection[0])
connector.commit()

mb.showinfo('Done', 'The record you wanted deleted was successfully


deleted.')

display_records()

def view_record():
global name_strvar, email_strvar, contact_strvar, gender_strvar, dob,
Branch_strvar

current_item = tree.focus()
values = tree.item(current_item)
selection = values["values"]

date = datetime.date(int(selection[5][:4]), int(selection[5][5:7]),


int(selection[5][8:]))

name_strvar.set(selection[1]); email_strvar.set(selection[2])
contact_strvar.set(selection[3]); gender_strvar.set(selection[4])
dob.set_date(date); Branch_strvar.set(selection[6])
# --------------------
# GUI CREATION
root = Tk()
root.title("DR.BR STUDENT MANAGEMENT SYSTEM")
root.minsize(1000,1000)
root.maxsize(1000,1000)
root.geometry("979x979")
#-------------------------
#background_section
lf_bg = 'orange'
cf_bg = 'brown'
#-------------------
#interface_creation
#creating stringvar and invar variable

name_strvar= StringVar()
contact_strvar= StringVar()
email_strvar= StringVar()
gender_strvar= StringVar()
Branch_strvar= StringVar()
#--------------------------------
#placing the componet in root window
Label(root,text= "College Management
System",font=headlabelfont,bg="orange",fg="white").pack(side=TOP,fill=X)
#----------------------------------------------------------------------------------
-------
#frames _creation
#left_frame
left_frame=Frame(root,bg="darkblue")
left_frame.place(x=0,y=30,relheight=1,relwidth=5)
#------------------
#-----creator detail
joshi_frame=Frame(root,bg="white")
joshi_frame.place(x=600,y=300,relheight=0.4,relwidth=0.4)
Label(joshi_frame,text="MADE
BY:",fg="black",bg="orange",font=labelfont).pack(side=LEFT,fill=X)

Label(joshi_frame,text="ANKUSHJOSHI",fg="green",bg="black",font=labelfont).pack(sid
e=LEFT,fill=X)
#--------------------------------
#center_frame
center_frame=Frame(root,bg="blue")
center_frame.place(x=400,y=30,relheight=1,relwidth=0.2)
Label(center_frame,text="HACKER
TEAM",fg="green",bg="yellow",font=labelfont).pack(side=TOP,fill=X)

#-----------------------------------------
# right_frame
right_frame=Frame(root,bg="skyblue")
right_frame.place(x=600,y=30,relheight=0.4,relwidth=0.4)
#--------------------------------------------
# placing the componets in the left frame
Label(left_frame, text="Name", font=labelfont, bg=lf_bg).place(relx=0, rely=0.05)
Label(left_frame,text="Contact
Number",font=labelfont,bg=lf_bg).place(relx=0,rely=0.11)
Label(left_frame,text="Email
Address",font=labelfont,bg=lf_bg).place(relx=0,rely=0.22)
Label(left_frame,text="Gender",font=labelfont,bg=lf_bg).place(relx=0,rely=0.30)
Label(left_frame,text="Date of Birth
(DOB)",font=labelfont,bg=lf_bg).place(relx=0,rely=0.37)
Label(left_frame,text="Branch",font=labelfont,bg=lf_bg).place(relx=0,rely=0.44)
#----------------------------------------------------------------------------------
#entry level
Entry(left_frame,textvariable=name_strvar,font=entryfont,bd=4).place(relx=0,rely=0.
08)
Entry(left_frame,textvariable=contact_strvar,font=entryfont,bd=4).place(relx=0,rely
=0.14)
Entry(left_frame,textvariable=email_strvar,font=entryfont,bd=4).place(relx=0,rely=0
.25)
Entry(left_frame,textvariable=Branch_strvar,font=entryfont,bd=4).place(relx=0,rely=
0.46)
#----------------------------------------------------------------------------------
----
# option_menu
OptionMenu(left_frame,gender_strvar,'Male','Female').place(x=0,relx=0,rely=0.32,rel
width=0.1)
#----------------------------------------------------------------------------------
--------
# date o birth (DOB)

dob = DateEntry(left_frame,font=("Arial",12),width=15)
dob.place(x=0,rely=0.40)
#----------------------------------------------------------------------------------
--------
# button left from submit
Button(center_frame,text="submit",font=labelfont,width=18,command=add_record).place
(relx=0.1,rely=0.07)
#----------------------------------------------------------------------------------
---------
# add button center frame
Button(center_frame,text="DELETE
RECORD",font=labelfont,width=15,command=remove_record).place(relx=0.1,rely=0.25)
Button(center_frame,text="View
RECORD",font=labelfont,width=15,command=view_record).place(relx=0.1,rely=0.35)
Button(center_frame,text="RESET
FIELD",font=labelfont,width=15,command=reset_fields).place(relx=0.1,rely=0.45)
Button(center_frame,text="DELETEDATABASE",font=labelfont,width=15,command=reset_for
m).place(relx=0.1,rely=0.55)
#----------------------------------------------------------------------------------
-----------
# PLACING Componet in Right Frame
Label(right_frame,text="STUDENT
DETAIL",font=headlabelfont,bg="black",fg="green").pack(side=TOP,fill=X)
tree = ttk.Treeview(right_frame,height=100,selectmode=BROWSE,
columns=('Student ID','Name','Email Address','Contact
Number','Gender','Date of Birth','Branch'))
X_scroller=Scrollbar(tree,orient=HORIZONTAL,command=tree.xview).pack(side=BOTTOM,fi
ll=X)
Y_scroller=Scrollbar(tree,orient=VERTICAL,command=tree.yview).pack(side=RIGHT,fill=
Y)

tree.config(yscrollcommand=Y_scroller, xscrollcommand=X_scroller)

tree.heading('Student ID', text='ID', anchor=CENTER)


tree.heading('Name', text='Name', anchor=CENTER)
tree.heading('Email Address', text='Email ID', anchor=CENTER)
tree.heading('Contact Number', text='Phone No', anchor=CENTER)
tree.heading('Gender', text='Gender', anchor=CENTER)
tree.heading('Date of Birth', text='DOB', anchor=CENTER)
tree.heading('Branch', text='Branch', anchor=CENTER)

tree.column('#0', width=0, stretch=NO)


tree.column('#1', width=40, stretch=NO)
tree.column('#2', width=140, stretch=NO)
tree.column('#3', width=200, stretch=NO)
tree.column('#4', width=80, stretch=NO)
tree.column('#5', width=80, stretch=NO)
tree.column('#6', width=80, stretch=NO)
tree.column('#7', width=150, stretch=NO)

tree.place(y=30, relwidth=1, relheight=0.9, relx=0)


display_records()
root.update()
#------------------------------------
#ENDING LOOP
root.mainloop()

You might also like