from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
import mysql.connector
from tkinter import messagebox
class Student:
def __init__(self,root):
self.root = root
self.root.geometry("1920x1080+0+0")
self.root.title("STUDENT MANAGEMENT SYSTEM")
# Variables
self.var_dep=StringVar()
self.var_course=StringVar()
self.var_year=StringVar()
self.var_semester=StringVar()
self.var_std_id=StringVar()
self.var_std_name=StringVar()
self.var_div=StringVar()
self.var_roll=StringVar()
self.var_gender=StringVar()
self.var_dob=StringVar()
self.var_email=StringVar()
self.var_phone=StringVar()
self.var_address=StringVar()
self.var_teacher=StringVar()
# bg image
img_4 = Image.open(r"D:THE BEST COLLEGEblack bg.jpeg")
img_4_resized = img_4.resize((1530, 790),Image.Resampling.LANCZOS)
self.photoimg_4 = ImageTk.PhotoImage(img_4_resized)
bg_lbl = Label(self.root, image=self.photoimg_4, bd=2, relief=RIDGE)
bg_lbl.place(x=0, y=0, width=1500, height=710)
# title
lbl_title = Label(bg_lbl, text="THE BEST GROUP OF COLLEGES", font=("times new roman", 40, "bold"), bg="grey", fg="blue")
lbl_title.place(x=0, y=0, width=1400, height=50)
# main frame
manage_frame = Frame(bg_lbl, bd=2, bg="grey", relief=RIDGE)
manage_frame.place(x=0, y=50, width=1500, height=670)
# left frame
DataLeftFrame = LabelFrame(manage_frame, bd=4, relief=RIDGE, padx=2, text="Student Data", font=("times new roman", 20, "bold"), bg="white", fg="red")
DataLeftFrame.place(x=10, y=10, width=620, height=580)
# Text
text_label = Label(DataLeftFrame, text="PLEASE FILL THE FOLLOWING FIELDS", font=("times new roman", 20, "bold"), bg="white", fg="blue")
text_label.place(x=0, y=0, width=620, height=120)
'''# image
img_5 = Image.open(r"D:THE BEST COLLEGEthe best college 3.jpeg")
img_5_resized = img_5.resize((1530, 790), Image.Resampling.LANCZOS)
self.photoimg_5 = ImageTk.PhotoImage(img_5_resized)
my_img = Label(DataLeftFrame, image=self.photoimg_4, bd=2, relief=RIDGE)
my_img.place(x=0, y=0, width=650, height=120)'''
#Current course LabelFrame Information
std_lbl_info_frame = LabelFrame(DataLeftFrame, bd=4, relief=RIDGE, padx=2, text="Current Course Information", font=("times new roman", 15, "bold"), bg="white", fg="red")
std_lbl_info_frame.place(x=0, y=120, width=620, height=150)
# Label and combobox
# department
lbl_dep = Label(std_lbl_info_frame, text="Department :", font=("arial", 12, "bold"), bg="white")
lbl_dep.grid(row=0, column=0, padx=2, pady=10, sticky=W)
combo_dep = ttk.Combobox(std_lbl_info_frame,textvariable= self.var_dep, font=("arial", 12, "bold"), width=17, state="readonly")
combo_dep["value"]=("Select Department", "Computer Science", "Information Technology", "Arts", "Law","linguistics")
combo_dep.current(0)
combo_dep.grid(row=0, column=1, padx=2, pady=10, sticky=W)
# course
course_std = Label(std_lbl_info_frame, text="Course :", font=("arial", 12, "bold"), bg="white")
course_std.grid(row=0, column=2, padx=15, pady=10, sticky=W)
combo_txtcourse = ttk.Combobox(std_lbl_info_frame,textvariable=self.var_course, font=("arial", 12, "bold"), width=17, state="readonly")
combo_txtcourse["value"]=("Select Course", "BSCS", "BSIT", "BA", "LLB","BS English","BS Urdu")
combo_txtcourse.current(0)
combo_txtcourse.grid(row=0, column=3, padx=10, pady=10, sticky=W)
# year
current_year = Label(std_lbl_info_frame, text="Year :", font=("arial", 12, "bold"), bg="white")
current_year.grid(row=1, column=0, padx=2, pady=10, sticky=W)
com_txt_current_year = ttk.Combobox(std_lbl_info_frame,textvariable=self.var_year ,font=("arial", 12, "bold"), width=17, state="readonly")
com_txt_current_year["value"]=("Select Year", "2020-21", "2021-22", "2022-23", "2023-24","2024-25", "2025-26", "2026-27", "2027-28", "2028-29", "2029-30", "2030-31")
com_txt_current_year.current(0)
com_txt_current_year.grid(row=1, column=1, padx=2, pady=10, sticky=W)
# semester
lbl_semester = Label(std_lbl_info_frame, text="Semester :", font=("arial", 12, "bold"), bg="white")
lbl_semester.grid(row=1, column=2, padx=15, pady=10, sticky=W)
combo_txt_semester = ttk.Combobox(std_lbl_info_frame,textvariable=self.var_semester, font=("arial", 12, "bold"), width=17, state="readonly")
combo_txt_semester["value"]=("Select Semester", "Semester-1", "Semester-2")
combo_txt_semester.current(0)
combo_txt_semester.grid(row=1, column=3, padx=10, pady=10, sticky=W)
#Student Class LabelFrame Information
std_lbl_class_frame = LabelFrame(DataLeftFrame, bd=4, relief=RIDGE, padx=2, text="Student Class Information", font=("times new roman", 15, "bold"), bg="white", fg="red")
std_lbl_class_frame.place(x=0, y=235, width=650, height=300)
# Label and entry
# student id
lbl_id = Label(std_lbl_class_frame, text="Student ID :", font=("arial", 12, "bold"),bg="white")
lbl_id.grid(row=0, column=0, padx=2, pady=7, sticky=W)
id_entry=ttk.Entry(std_lbl_class_frame,textvariable=self.var_std_id, width=15, font=("arial", 12, "bold"))
id_entry.grid(row=0, column=1, padx=2, pady=7, sticky=W)
#name
lbl_Name = Label(std_lbl_class_frame, text="Student Name :", font=("arial", 12, "bold"), bg="white")
lbl_Name.grid(row=0, column=2, padx=2, pady=7, sticky=W)
txt_name=ttk.Entry(std_lbl_class_frame,textvariable=self.var_std_name, width=15, font=("arial", 12, "bold"))
txt_name.grid(row=0, column=3, padx=2, pady=7, sticky=W)
#class Division
lbl_class = Label(std_lbl_class_frame, text="Class Division :", font=("arial", 12, "bold"), bg="white")
lbl_class.grid(row=1, column=0, padx=2, pady=7, sticky=W)
combo_txt_class = ttk.Combobox(std_lbl_class_frame,textvariable=self.var_div, font=("arial", 12, "bold"), width=18, state="readonly")
combo_txt_class["value"]=("Select Class", "1st Year", "2nd Year", "3rd Year", "4th Year")
combo_txt_class.current(0)
combo_txt_class.grid(row=1, column=1, padx=2, pady=7, sticky=W)
#Roll no
lbl_roll = Label(std_lbl_class_frame, text="Roll No :", font=("arial", 12, "bold"), bg="white")
lbl_roll.grid(row=1, column=2, padx=5, pady=7, sticky=W)
txt_roll=ttk.Entry(std_lbl_class_frame,textvariable= self.var_roll, width=15, font=("arial", 12, "bold"))
txt_roll.grid(row=1, column=3, padx=2, pady=7, sticky=W)
#gender
lbl_gender = Label(std_lbl_class_frame, text="Gender :", font=("arial", 12, "bold"), bg="white")
lbl_gender.grid(row=2, column=0, padx=2, pady=7, sticky=W)
combo_txt_gender = ttk.Combobox(std_lbl_class_frame,textvariable= self.var_gender, font=("arial", 12, "bold"), width=18, state="readonly")
combo_txt_gender["value"]=("Select Gender", "Male", "Female", "Other")
combo_txt_gender.current(0)
combo_txt_gender.grid(row=2, column=1, padx=2, pady=7, sticky=W)
#dob
lbl_dob = Label(std_lbl_class_frame, text="DOB :", font=("arial", 12, "bold"), bg="white")
lbl_dob.grid(row=2, column=2, padx=5, pady=7, sticky=W)
txt_dob=ttk.Entry(std_lbl_class_frame,textvariable=self.var_dob, width=15, font=("arial", 12, "bold"))
txt_dob.grid(row=2, column=3, padx=2, pady=7, sticky=W)
#email
lbl_email = Label(std_lbl_class_frame, text="Email :", font=("arial", 12, "bold"), bg="white")
lbl_email.grid(row=3, column=0, padx=2, pady=7, sticky=W)
txt_email=ttk.Entry(std_lbl_class_frame,textvariable= self.var_email, width=15, font=("arial", 12, "bold"))
txt_email.grid(row=3, column=1, padx=2, pady=7, sticky=W)
#phone
lbl_phone = Label(std_lbl_class_frame, text="Phone :", font=("arial", 12, "bold"), bg="white")
lbl_phone.grid(row=3, column=2, padx=5, pady=7, sticky=W)
txt_phone=ttk.Entry(std_lbl_class_frame,textvariable=self.var_phone, width=15, font=("arial", 12, "bold"))
txt_phone.grid(row=3, column=3, padx=2, pady=7, sticky=W)
#address
lbl_address = Label(std_lbl_class_frame, text="Address :", font=("arial", 12, "bold"), bg="white")
lbl_address.grid(row=4, column=0, padx=2, pady=7, sticky=W)
txt_address=ttk.Entry(std_lbl_class_frame,textvariable= self.var_address, width=15, font=("arial", 12, "bold"))
txt_address.grid(row=4, column=1, padx=2, pady=7, sticky=W)
#teacher
lbl_teacher = Label(std_lbl_class_frame, text="Teacher :", font=("arial", 12, "bold"), bg="white")
lbl_teacher.grid(row=4, column=2, padx=3, pady=7, sticky=W)
txt_teacher=ttk.Entry(std_lbl_class_frame,textvariable=self.var_teacher, width=15, font=("arial", 12, "bold"))
txt_teacher.grid(row=4, column=3, padx=2, pady=7, sticky=W)
#button frame
btn_frame = Frame(DataLeftFrame, bd=2, relief=RIDGE, bg="white")
btn_frame.place(x=0, y=470, width=650, height=38)
btn_Add=Button(btn_frame, text="Save",command=self.add_data, width=14, font=("arial", 12, "bold"), bg="blue", fg="white")
btn_Add.grid(row=0, column=0, padx=1)
btn_Update=Button(btn_frame, text="Update",command=self.update_data, width=14, font=("arial", 12, "bold"), bg="blue", fg="white")
btn_Update.grid(row=0, column=1, padx=1)
btn_Delete=Button(btn_frame, text="Delete",command=self.delete_data, width=14, font=("arial", 12, "bold"), bg="blue", fg="white")
btn_Delete.grid(row=0, column=2, padx=1)
btn_Reset=Button(btn_frame, text="Reset",command=self.reset_data, width=14, font=("arial", 12, "bold"), bg="blue", fg="white")
btn_Reset.grid(row=0, column=3, padx=1)
# right frame
DataRightFrame = LabelFrame(manage_frame, bd=4, relief=RIDGE, padx=2, text="Student Information", font=("times new roman", 20, "bold"), bg="white", fg="red")
DataRightFrame.place(x=630, y=10, width=620, height=580)
#img1
img_6 = Image.open(r"D:THE BEST COLLEGEthe best college 1.jpeg")
img_6_resized = img_6.resize((600, 130), Image.Resampling.LANCZOS)
self.photoimg_6 = ImageTk.PhotoImage(img_6_resized)
my_img = Label(DataRightFrame, image=self.photoimg_6)
my_img.place(x=0, y=0, width=600, height=130)
# right frame
Search_Frame= LabelFrame(DataRightFrame, bd=4, relief=RIDGE, padx=2, text="Search Student Information", font=("times new roman", 15, "bold"), bg="white", fg="red")
Search_Frame.place(x=0, y=130, width=600, height=65)
search_by = Label(Search_Frame, text="Search By", font=("arial", 12, "bold"), bg="white")
search_by.grid(row=0, column=0, padx=5, pady=7, sticky=W)
#Search
self.var_com_search = StringVar()
combo_txt_search = ttk.Combobox(Search_Frame,textvariable=self.var_com_search, font=("arial", 12, "bold"), width=11, state="readonly")
combo_txt_search["value"]=("Select Option", "Roll No", "Phone No","student_id")
combo_txt_search.current(0)
combo_txt_search.grid(row=0, column=1, padx=5, pady=7, sticky=W)
self.var_search=StringVar()
txt_search = ttk.Entry(Search_Frame,textvariable=self.var_search, width=13, font=("arial", 12, "bold"))
txt_search.grid(row=0, column=2, padx=5, sticky=W)
btn_search = Button(Search_Frame,command=self.search_data, text="Search", width=10, font=("arial", 12, "bold"), bg="blue", fg="white")
btn_search.grid(row=0, column=3, padx=1)
btn_ShowAll = Button(Search_Frame,command=self.fetch_data, text="Show All", width=10, font=("arial", 12, "bold"), bg="blue", fg="white")
btn_ShowAll.grid(row=0, column=4, padx=1)
# student table and scroll bar
table_frame = Frame(DataRightFrame, bd=4, relief=RIDGE, bg="white")
table_frame.place(x=0, y=200, width=600, height=300)
scroll_x = ttk.Scrollbar(table_frame, orient=HORIZONTAL)
scroll_y = ttk.Scrollbar(table_frame, orient=VERTICAL)
self.students_table = ttk.Treeview(table_frame, column=("dep", "course", "year", "sem", "id", "name", "div", "roll", "gender", "dob", "email", "phone", "address", "teacher"), xscrollcommand=scroll_x.set, yscrollcommand=scroll_y.set)
scroll_x.pack(side=BOTTOM, fill=X)
scroll_y.pack(side=RIGHT, fill=Y)
scroll_x.config(command=self.students_table.xview)
scroll_y.config(command=self.students_table.yview)
self.students_table.heading("dep",text="Department")
self.students_table.heading("course",text="Course")
self.students_table.heading("year",text="Year")
self.students_table.heading("sem",text="Semester")
self.students_table.heading("id",text="StudentId")
self.students_table.heading("name",text="Student Name")
self.students_table.heading("div",text="Class Division")
self.students_table.heading("roll",text="Roll No")
self.students_table.heading("gender",text="Gender")
self.students_table.heading("dob",text="DOB")
self.students_table.heading("email",text="Email")
self.students_table.heading("phone",text="Phone No")
self.students_table.heading("address",text="Address")
self.students_table.heading("teacher",text="Teacher")
self.students_table["show"] = "headings"
self.students_table.column("dep",width=100)
self.students_table.column("course",width=100)
self.students_table.column("year",width=100)
self.students_table.column("sem",width=100)
self.students_table.column("id",width=100)
self.students_table.column("name",width=100)
self.students_table.column("div",width=100)
self.students_table.column("roll",width=100)
self.students_table.column("gender",width=100)
self.students_table.column("dob",width=100)
self.students_table.column("email",width=100)
self.students_table.column("phone",width=100)
self.students_table.column("address",width=100)
self.students_table.column("teacher",width=100)
self.students_table.pack(fill=BOTH, expand=1)
self.fetch_data()
self.students_table.bind("<ButtonRelease-1>", self.get_cursor)
def add_data(self):
if self.var_dep.get() == "Select Department" or self.var_std_name.get() == "" or self.var_std_id.get() == "":
messagebox.showerror("Error", "All fields are required", parent=self.root)
else:
try:
conn = mysql.connector.connect(host="localhost", username="root", password="Alphamale1.", database="the_best_college")
my_cursor = conn.cursor()
my_cursor.execute("insert into students values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(
self.var_dep.get(),
self.var_course.get(),
self.var_year.get(),
self.var_semester.get(),
self.var_std_id.get(),
self.var_std_name.get(),
self.var_div.get(),
self.var_roll.get(),
self.var_gender.get(),
self.var_dob.get(),
self.var_email.get(),
self.var_phone.get(),
self.var_address.get(),
self.var_teacher.get(),)
)
conn.commit()
self.fetch_data()
conn.close()
messagebox.showinfo("Success", "Student details has been added successfully", parent=self.root)
except Exception as es:
messagebox.showerror("Error", f"Due To : {str(es)}", parent=self.root)
# fetch function
def fetch_data(self):
conn = mysql.connector.connect(host="localhost", username="root", password="Alphamale1.", database="the_best_college")
my_cursor = conn.cursor()
my_cursor.execute("select * from students")
data = my_cursor.fetchall()
if len(data) != 0:
self.students_table.delete(*self.students_table.get_children())
for i in data:
self.students_table.insert("", END, values=i)
conn.commit()
conn.close()
# Get Cursor
def get_cursor(self, event=""):
cursor_row = self.students_table.focus()
content = self.students_table.item(cursor_row)
data = content["values"]
self.var_dep.set(data[0]),
self.var_course.set(data[1]),
self.var_year.set(data[2]),
self.var_semester.set(data[3]),
self.var_std_id.set(data[4]),
self.var_std_name.set(data[5]),
self.var_div.set(data[6]),
self.var_roll.set(data[7]),
self.var_gender.set(data[8]),
self.var_dob.set(data[9]),
self.var_email.set(data[10]),
self.var_phone.set(data[11]),
self.var_address.set(data[12]),
self.var_teacher.set(data[13]),
# Update Function
def update_data(self):
if self.var_dep.get() == "Select Department" or self.var_std_name.get() == "" or self.var_std_id.get() == "":
messagebox.showerror("Error", "All fields are required", parent=self.root)
else:
try:
Update = messagebox.askyesno("Update", "Do you want to update this student details", parent=self.root)
if Update > 0:
conn = mysql.connector.connect(host="localhost", username="root", password="Alphamale1.", database="the_best_college")
my_cursor = conn.cursor()
my_cursor.execute("update students set Dep=%s, Course=%s, Year=%s, Semester=%s, Name=%s, Division=%s, Roll=%s, Gender=%s, Dob=%s, Email=%s, Phone=%s, Address=%s, Teacher=%s where Student_id=%s",(
self.var_dep.get(),
self.var_course.get(),
self.var_year.get(),
self.var_semester.get(),
self.var_std_name.get(),
self.var_div.get(),
self.var_roll.get(),
self.var_gender.get(),
self.var_dob.get(),
self.var_email.get(),
self.var_phone.get(),
self.var_address.get(),
self.var_teacher.get(),
self.var_std_id.get(),))
else:
if not Update:
return
messagebox.showinfo("Success", "Student details successfully updated", parent=self.root)
conn.commit()
self.fetch_data()
conn.close()
except Exception as es:
messagebox.showerror("Error", f"Due To : {str(es)}", parent=self.root)
# Delete Function
def delete_data(self):
if self.var_std_id.get() == "":
messagebox.showerror("Error", "Student id must be required", parent=self.root)
else:
try:
delete = messagebox.askyesno("Delete", "Are you sure you want to delete this student", parent=self.root)
if delete > 0:
conn = mysql.connector.connect(host="localhost", username="root", password="Alphamale1.", database="the_best_college")
my_cursor = conn.cursor()
sql = "delete from students where Student_id=%s"
value = (self.var_std_id.get(),)
my_cursor.execute(sql, value)
else:
if not delete:
return
conn.commit()
self.fetch_data()
conn.close()
messagebox.showinfo("Delete", "Student details successfully deleted", parent=self.root)
except Exception as es:
messagebox.showerror("Error", f"Due To : {str(es)}", parent=self.root)
# Reset Function
def reset_data(self):
self.var_dep.set("Select Department")
self.var_course.set("Select Course")
self.var_year.set("Select Year")
self.var_semester.set("Select Semester")
self.var_std_id.set("")
self.var_std_name.set("")
self.var_div.set("Select Division")
self.var_roll.set("")
self.var_gender.set("Male")
self.var_dob.set("")
self.var_email.set("")
self.var_phone.set("")
self.var_address.set("")
self.var_teacher.set("")
# Search Function
def search_data(self):
if self.var_com_search.get() == "" or self.var_search.get() == "":
messagebox.showerror("Error", "Select option")
else:
try:
conn = mysql.connector.connect(host="localhost", username="root", password="Alphamale1.", database="the_best_college")
my_cursor = conn.cursor()
my_cursor.execute("SELECT * FROM students WHERE {} LIKE %s".format(self.var_com_search.get()), ('%' + self.var_search.get() + '%',))
rows = my_cursor.fetchall()
if len(rows) != 0:
self.students_table.delete(*self.students_table.get_children())
for i in rows:
self.students_table.insert("", END, values=i)
conn.commit()
conn.close()
except Exception as es:
messagebox.showerror("Error", f"Due To : {str(es)}", parent=self.root)
if __name__ == "__main__":
root = Tk()
obj = Student(root)
root.mainloop()
**
This is my python code and this is the code in mysql:**
CREATE TABLE IF NOT EXISTS `the_best_college`.`students` (
`Dep` VARCHAR(50),
`Course` VARCHAR(50),
`Year` VARCHAR(50),
`Semester` VARCHAR(50),
`Student_id` INT AUTO_INCREMENT PRIMARY KEY,
`Name` VARCHAR(100),
`Division` VARCHAR(50),
`Roll` VARCHAR(50),
`Gender` VARCHAR(20),
`Dob` VARCHAR(50),
`Email` VARCHAR(100),
`Phone` VARCHAR(15),
`Address` VARCHAR(255),
`Teacher` VARCHAR(100)
);
I want to Create more than one tables so that, I can use joins and Constraints. What changes do i have to make in my python code?
I tried to create three tables according to my need but when i tried to run my project it did not worked because of that modification
New contributor
Harib Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.