0% found this document useful (0 votes)
12 views

Sample OOP

Python Code

Uploaded by

evanlugo96
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)
12 views

Sample OOP

Python Code

Uploaded by

evanlugo96
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/ 6

import tkinter as tk

from tkinter import ttk, messagebox


import sqlite3

class StudentRegistrationApp:
def __init__(self, root):
self.root = root
self.root.title("Student Registration")
self.root.geometry("950x700")
self.root.resizable(False, False)

# Variable declarations
self.studid = tk.StringVar()
self.lastname = tk.StringVar()
self.firstname = tk.StringVar()
self.middlename = tk.StringVar()
self.course = tk.StringVar()
self.birthdate = tk.StringVar()
self.sex = tk.StringVar()
self.address = tk.StringVar()
self.phoneNumber = tk.StringVar()
self.email = tk.StringVar()
self.name = tk.StringVar()
self.relationship = tk.StringVar()
self.contactNumber = tk.StringVar()

self.create_widgets()
self.create_table()
self.load_data()

def create_widgets(self):
# Create the data grid
self.tree = ttk.Treeview(self.root, columns=('cID', 'clastname',
'cfirstname', 'cmiddlename', 'ccourse', 'cbirthdate', 'csex', 'caddress',
'cphonenumber', 'cemail', 'crelationship', 'ccontactnumber'), show='headings')
self.tree.heading('cID', text='ID')
self.tree.heading('clastname', text='Lastname')
self.tree.heading('cfirstname', text='Firstname')
self.tree.heading('cmiddlename', text='Middlename')
self.tree.heading('ccourse', text='Course')
self.tree.heading('cbirthdate', text='Birthdate')
self.tree.heading('csex', text='Sex')
self.tree.heading('caddress', text='Address')
self.tree.heading('cphonenumber', text='Phone Number')
self.tree.heading('cemail', text='Email')
self.tree.heading('crelationship', text='Relationship')
self.tree.heading('ccontactnumber', text='Contact Number')

# Set width of columns


self.tree.column('cID', width=50)
self.tree.column('clastname', width=75)
self.tree.column('cfirstname', width=75)
self.tree.column('cmiddlename', width=75)
self.tree.column('ccourse', width=60)
self.tree.column('cbirthdate', width=65)
self.tree.column('csex', width=50)
self.tree.column('caddress', width=90)
self.tree.column('cphonenumber', width=90)
self.tree.column('cemail', width=75)
self.tree.column('crelationship', width=75)
self.tree.column('ccontactnumber', width=100)

self.tree.grid(row=1, column=0, sticky="nsew", padx=10, pady=430)

# Create labels and entry fields


tk.Label(self.root, text="STUDENT REGISTRATION", font=("Tahoma", 24,
"bold")).place(x=200, y=10)
tk.Label(self.root, text="ID Number: ", font=("Tahoma", 12,
"bold")).place(x=30, y=70)
tk.Label(self.root, text="Fullname: ", font=("Tahoma", 12,
"bold")).place(x=30, y=100)
tk.Label(self.root, text="Lastname", font=("Tahoma", 12,
"bold")).place(x=170, y=130)
tk.Label(self.root, text="Firstname", font=("Tahoma", 12,
"bold")).place(x=380, y=130)
tk.Label(self.root, text="Middlename", font=("Tahoma", 12,
"bold")).place(x=580, y=130)
tk.Label(self.root, text="Course:", font=("Tahoma", 12,
"bold")).place(x=30, y=160)
tk.Label(self.root, text="Birthdate:", font=("Tahoma", 12,
"bold")).place(x=30, y=190)
tk.Label(self.root, text="Sex:", font=("Tahoma", 12, "bold")).place(x=420,
y=190)
tk.Label(self.root, text="Address:", font=("Tahoma", 12,
"bold")).place(x=30, y=220)
tk.Label(self.root, text="Phone Number:", font=("Tahoma", 12,
"bold")).place(x=30, y=250)
tk.Label(self.root, text="Email:", font=("Tahoma", 12, "bold")).place(x=30,
y=280)
tk.Label(self.root, text="Emergency Contact Details:", font=("Tahoma", 12,
"bold")).place(x=30, y=310)
tk.Label(self.root, text="Name:", font=("Tahoma", 12, "bold")).place(x=30,
y=340)
tk.Label(self.root, text="Relationship:", font=("Tahoma", 12,
"bold")).place(x=30, y=370)
tk.Label(self.root, text="Contact Number:", font=("Tahoma", 12,
"bold")).place(x=30, y=400)

# Create entries
tk.Entry(self.root, textvariable=self.studid, font=("Tahoma", 12, "bold"),
validate="key", validatecommand=(self.root.register(self.val_input_numbers),
"%S")).place(x=135, y=70)
tk.Entry(self.root, textvariable=self.lastname, font=("Tahoma", 12,
"bold"), validate="key",
validatecommand=(self.root.register(self.val_input_letters), "%S")).place(x=120,
y=100)
tk.Entry(self.root, textvariable=self.firstname, font=("Tahoma", 12,
"bold"), validate="key",
validatecommand=(self.root.register(self.val_input_letters), "%S")).place(x=320,
y=100)
tk.Entry(self.root, textvariable=self.middlename, font=("Tahoma", 12,
"bold"), validate="key",
validatecommand=(self.root.register(self.val_input_letters), "%S")).place(x=520,
y=100)
tk.Entry(self.root, textvariable=self.course, font=("Tahoma", 12,
"bold")).place(x=175, y=160)
tk.Entry(self.root, textvariable=self.birthdate, font=("Tahoma", 12,
"bold")).place(x=175, y=190)
tk.Entry(self.root, textvariable=self.sex, font=("Tahoma", 12, "bold"),
validate="key", validatecommand=(self.root.register(self.val_input_letters),
"%S")).place(x=490, y=190)
tk.Entry(self.root, textvariable=self.address, font=("Tahoma", 12,
"bold")).place(x=175, y=220)
tk.Entry(self.root, textvariable=self.phoneNumber, font=("Tahoma", 12,
"bold"), validate="key",
validatecommand=(self.root.register(self.val_input_numbers), "%S")).place(x=175,
y=250)
tk.Entry(self.root, textvariable=self.email, font=("Tahoma", 12,
"bold")).place(x=175, y=280)
tk.Entry(self.root, textvariable=self.name, font=("Tahoma", 12, "bold"),
validate="key", validatecommand=(self.root.register(self.val_input_letters),
"%S")).place(x=175, y=340)
tk.Entry(self.root, textvariable=self.relationship, font=("Tahoma", 12,
"bold"), validate="key",
validatecommand=(self.root.register(self.val_input_letters), "%S")).place(x=175,
y=370)
tk.Entry(self.root, textvariable=self.contactNumber, font=("Tahoma", 12,
"bold"), validate="key",
validatecommand=(self.root.register(self.val_input_numbers), "%S")).place(x=175,
y=400)

# Create command buttons


tk.Button(self.root, text="Add New", font=("Tahoma", 12, "bold"), width=10,
height=2, command=self.add_new).place(x=420, y=250)
tk.Button(self.root, text="Save", font=("Tahoma", 12, "bold"), width=10,
height=2, command=self.save_record).place(x=530, y=250)
tk.Button(self.root, text="Edit", font=("Tahoma", 12, "bold"), width=10,
height=2, command=self.edit).place(x=640, y=250)
tk.Button(self.root, text="Update", font=("Tahoma", 12, "bold"), width=10,
height=2, command=self.update_record).place(x=750, y=250)
tk.Button(self.root, text="Delete", font=("Tahoma", 12, "bold"), width=10,
height=2, command=self.delete_record).place(x=475, y=302)
tk.Button(self.root, text="Cancel", font=("Tahoma", 12, "bold"), width=10,
height=2, command=self.cancel).place(x=585, y=302)
tk.Button(self.root, text="Close", font=("Tahoma", 12, "bold"), width=10,
height=2, command=self.root.quit).place(x=695, y=302)

def create_table(self):
conn = sqlite3.connect("studentRegistration.db")
with conn:
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS Student(
StudentID TEXT,
Lastname TEXT,
Firstname TEXT,
Middlename TEXT,
Course TEXT,
Birthdate TEXT,
Sex TEXT,
Address TEXT,
PhoneNumber TEXT,
Email TEXT,
Name TEXT,
Relationship TEXT,
ContactNumber TEXT
)
""")
conn.commit()

def load_data(self):
for item in self.tree.get_children():
self.tree.delete(item)

conn = sqlite3.connect("studentRegistration.db")
with conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM Student")
rows = cursor.fetchall()
for row in rows:
self.tree.insert("", "end", values=row)

def add_new(self):
self.studid.set("")
self.lastname.set("")
self.firstname.set("")
self.middlename.set("")
self.course.set("")
self.birthdate.set("")
self.sex.set("")
self.address.set("")
self.phoneNumber.set("")
self.email.set("")
self.name.set("")
self.relationship.set("")
self.contactNumber.set("")

def val_input_numbers(self, text):


return all(char.isdigit() or char == "-" for char in text)

def val_input_letters(self, text):


return all(char.isalpha() or char.isspace() for char in text)

def cancel(self):
self.add_new()

def save_record(self):
id = self.studid.get()
lname = self.lastname.get()
fname = self.firstname.get()
mname = self.middlename.get()
crs = self.course.get()
bdate = self.birthdate.get()
sx = self.sex.get()
add = self.address.get()
pnumber = self.phoneNumber.get()
em = self.email.get()
nm = self.name.get()
rel = self.relationship.get()
cnumber = self.contactNumber.get()

for child in self.tree.get_children():


if id == self.tree.item(child, "values")[0]:
messagebox.showerror("Error", f" Student ID {id} already exists!")
return
conn = sqlite3.connect("studentRegistration.db")
with conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM Student WHERE Lastname=? AND Firstname=?
AND Middlename=?", (lname, fname, mname))
if cursor.fetchone():
messagebox.showerror("Error", f"Record {lname}, {fname}, {mname}
already exists")
return

self.tree.insert("", "end", values=(id, lname, fname, mname, crs, bdate,


sx, add, pnumber, em, nm, rel, cnumber))

with conn:
cursor.execute("INSERT INTO Student VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (id, lname, fname, mname, crs, bdate, sx,
add, pnumber, em, nm, rel, cnumber))
conn.commit()

messagebox.showinfo("Save", f"Record {fname} has been successfully saved")

def edit(self):
selected_item = self.tree.selection()
if not selected_item:
messagebox.showerror("Error", "Please select a record to edit.")
return

values = self.tree.item(selected_item)['values']
self.studid.set(values[0])
self.lastname.set(values[1])
self.firstname.set(values[2])
self.middlename.set(values[3])
self.course.set(values[4])
self.birthdate.set(values[5])
self.sex.set(values[6])
self.address.set(values[7])
self.phoneNumber.set(values[8])
self.email.set(values[9])
self.name.set(values[10])
self.relationship.set(values[11])
self.contactNumber.set(values[12])

def update_record(self):
selected_item = self.tree.selection()
if not selected_item:
messagebox.showerror("Error", "Please select a record to update.")
return

id = self.studid.get()
lname = self.lastname.get()
fname = self.firstname.get()
mname = self.middlename.get()
crs = self.course.get()
bdate = self.birthdate.get()
sx = self.sex.get()
add = self.address.get()
pnumber = self.phoneNumber.get()
em = self.email.get()
nm = self.name.get()
rel = self.relationship.get()
cnumber = self.contactNumber.get()

conn = sqlite3.connect("studentRegistration.db")
with conn:
cursor = conn.cursor()
cursor.execute("""
UPDATE Student
SET Lastname = ?, Firstname = ?, Middlename = ?, Course = ?,
Birthdate = ?, Sex = ?, Address = ?, PhoneNumber = ?, Email = ?, Name = ?,
Relationship = ?, ContactNumber = ?
WHERE StudentID = ?
""", (lname, fname, mname, crs, bdate, sx, add, pnumber, em, nm, rel,
cnumber, id))
conn.commit()

self.tree.item(selected_item, values=(id, lname, fname, mname, crs, bdate,


sx, add, pnumber, em, nm, rel, cnumber))

messagebox.showinfo("Update", f"The record of {fname} has been successfully


updated.")

def delete_record(self):
selected_item = self.tree.selection()
if not selected_item:
messagebox.showerror("Error", "Please select a record to delete.")
return

confirm = messagebox.askyesno("Confirmation", "Are you sure you want to


delete the record?")
if confirm:
stud_id = self.tree.item(selected_item, "values")[0]

conn = sqlite3.connect("studentRegistration.db")
with conn:
cursor = conn.cursor()
cursor.execute("DELETE FROM Student WHERE StudentID = ?",
(stud_id,))
conn.commit()

self.tree.delete(selected_item)
messagebox.showinfo("Delete", "Record has been successfully deleted!")

if __name__ == "__main__":
root = tk.Tk()
app = StudentRegistrationApp(root)
root.mainloop()

You might also like