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

Code 3

This document describes a Python program that creates a GUI for a preschool information system using Tkinter. The program connects to a MySQL database called "students_db" and allows the user to perform CRUD operations on student records in a table. It displays the records in a treeview widget and includes buttons to add, update, delete, search and get the average grade of selected students. Placeholder variables and functions are used to populate and manipulate the entries and tree view.

Uploaded by

Nuriel Aguilar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Code 3

This document describes a Python program that creates a GUI for a preschool information system using Tkinter. The program connects to a MySQL database called "students_db" and allows the user to perform CRUD operations on student records in a table. It displays the records in a treeview widget and includes buttons to add, update, delete, search and get the average grade of selected students. Placeholder variables and functions are used to populate and manipulate the entries and tree view.

Uploaded by

Nuriel Aguilar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#pip install PyMySQL

from ast import Add


import pymysql
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import tkinter as tk

#Connection for phpmyadmin (MySQL)


def connection():
conn = pymysql.connect(
host='localhost',
user='root',
password='',
db='students_db',
)
return conn

def refreshTable():
for data in my_tree.get_children():
my_tree.delete(data)

for array in read():


my_tree.insert(parent='', index='end', iid=array, text="", values=(array),
tag="orow")

my_tree.tag_configure('orow', background='#EEEEEE', font=('Arial', 12))


my_tree.grid(row=8, column=0, columnspan=5, rowspan=11, padx=10, pady=20)

root = Tk()
root.title("Preschool Information System")
root.geometry("1080x720")
root.config(bg="#77ff68")
my_tree = ttk.Treeview(root)

#Placeholders for entry


ph1 = tk.StringVar()
ph2 = tk.StringVar()
ph3 = tk.StringVar()
ph4 = tk.StringVar()
ph5 = tk.StringVar()

#Placeholder set value function


def setph(word,num):
if num ==1:
ph1.set(word)
if num ==2:
ph2.set(word)
if num ==3:
ph3.set(word)
if num ==4:
ph4.set(word)
if num ==5:
ph5.set(word)

#Functions of the program


def read():
conn = connection()
cursor = conn.cursor()
cursor.execute("SELECT * FROM students")
results = cursor.fetchall()
conn.commit()
conn.close()
return results

#Add record
def add():
studid = str(studidEntry.get())
fname = str(fnameEntry.get())
scigrade = str(scigradeEntry.get())
mtgrade = str(mtgradeEntry.get())
enggrade = str(enggradeEntry.get())

if (studid == "" or studid == " ") or (fname == "" or fname == " ") or
(scigrade == "" or scigrade == " ") or (mtgrade == "" or mtgrade == " ") or
(enggrade == "" or enggrade == " "):
messagebox.showinfo("Error", "Please fill up the blank entry")
return
else:
try:
conn = connection()
cursor = conn.cursor()
cursor.execute("INSERT INTO students VALUES
('"+studid+"','"+fname+"','"+scigrade+"','"+mtgrade+"','"+enggrade+"') ")
conn.commit()
conn.close()
except:
messagebox.showinfo("Error", "Student ID already exist")
return

refreshTable()

#Delete record
def delete():
decision = messagebox.askquestion("Warning", "Do you want to delete the
selected data?")
if decision != "yes":
return
else:
selected_item = my_tree.selection()[0]
deleteData = str(my_tree.item(selected_item)['values'][0])
try:
conn = connection()
cursor = conn.cursor()
cursor.execute("DELETE FROM students WHERE STUDID='"+str(deleteData)
+"'")
conn.commit()
conn.close()
except:
messagebox.showinfo("Error", "Sorry an error occured")
return

refreshTable()

#Select record
def select():
try:
selected_item = my_tree.selection()[0]
studid = str(my_tree.item(selected_item)['values'][0])
fname = str(my_tree.item(selected_item)['values'][1])
scigrade = str(my_tree.item(selected_item)['values'][2])
mtgrade = str(my_tree.item(selected_item)['values'][3])
enggrade = str(my_tree.item(selected_item)['values'][4])

setph(studid,1)
setph(fname,2)
setph(scigrade,3)
setph(mtgrade,4)
setph(enggrade,5)

except:
messagebox.showinfo("Error", "Please select a data row")

#Search record
def search():
studid = str(studidEntry.get())
fname = str(fnameEntry.get())
scigrade = str(scigradeEntry.get())
mtgrade = str(mtgradeEntry.get())
enggrade = str(enggradeEntry.get())

conn = connection()
cursor = conn.cursor()
cursor.execute("SELECT * FROM students WHERE STUDID='"+
studid+"' or FNAME='"+
fname+"' or SCIGRADE='"+
scigrade+"' or MTGRADE='"+
mtgrade+"' or ENGGRADE='"+
enggrade+"' ")

try:
result = cursor.fetchall()

for num in range(0,5):


setph(result[0][num],(num+1))

conn.commit()
conn.close()
except:
messagebox.showinfo("Error", "No data found")

def update():
selectedStudid = ""

try:
selected_item = my_tree.selection()[0]
selectedStudid = str(my_tree.item(selected_item)['values'][0])
except:
messagebox.showinfo("Error", "Please select a data row")

studid = str(studidEntry.get())
fname = str(fnameEntry.get())
scigrade = str(scigradeEntry.get())
mtgrade = str(mtgradeEntry.get())
enggrade = str(enggradeEntry.get())
if (studid == "" or studid == " ") or (fname == "" or fname == " ") or
(scigrade == "" or scigrade == " ") or (mtgrade == "" or mtgrade == " ") or
(enggrade == "" or enggrade == " "):
messagebox.showinfo("Error", "Please fill up the blank entry")
return
else:
try:
conn = connection()
cursor = conn.cursor()
cursor.execute("UPDATE students SET STUDID='"+
studid+"', FNAME='"+
fname+"', SCIGRADE='"+
scigrade+"', MTGRADE='"+
mtgrade+ "', ENGGRADE='"+
enggrade+ "' WHERE STUDID='"+
selectedStudid+"' ")
conn.commit()
conn.close()
except:
messagebox.showinfo("Error", "Student ID already exist")
return

refreshTable()

def average():
try:
scigrade = int(scigradeEntry.get())
mtgrade = int(mtgradeEntry.get())
enggrade = int(enggradeEntry.get())

if (scigrade == "" or scigrade == " ") or (mtgrade == "" or mtgrade == " ")
or (enggrade == "" or enggrade == " "):
messagebox.showinfo("Error", "No input")
else:
try:
gr = (scigrade, mtgrade, enggrade)
avg = sum(gr)/3
messagebox.showinfo("The average is", avg)
except:
messagebox.showinfo("Error", "No input")
return
except:
messagebox.showinfo("Error", "No input")

refreshTable()

#Title bar of program


label = Label(root, text="Preschool Information System", font=('Arial Bold', 30),
bg='#77ff68')
label.grid(row=0, column=0, columnspan=8, rowspan=2, padx=50, pady=40)

#Text, font, placing for the label of input box


studidLabel = Label(root, text="Student ID", font=('Arial', 15), bg='#77ff68')
fnameLabel = Label(root, text="Full Name", font=('Arial', 15), bg='#77ff68')
scigradeLabel = Label(root, text="Science", font=('Arial', 15), bg='#77ff68')
mtgradeLabel = Label(root, text="Math", font=('Arial', 15), bg='#77ff68')
enggradeLabel = Label(root, text="English", font=('Arial', 15), bg='#77ff68')
studidLabel.grid(row=3, column=0, columnspan=1, padx=50, pady=5)
fnameLabel.grid(row=4, column=0, columnspan=1, padx=50, pady=5)
scigradeLabel.grid(row=5, column=0, columnspan=1, padx=50, pady=5)
mtgradeLabel.grid(row=6, column=0, columnspan=1, padx=50, pady=5)
enggradeLabel.grid(row=7, column=0, columnspan=1, padx=50, pady=5)

#Text, font, placing of input box


studidEntry = Entry(root, width=55, bd=1, font=('Arial', 15), textvariable = ph1)
fnameEntry = Entry(root, width=55, bd=1, font=('Arial', 15), textvariable = ph2)
scigradeEntry = Entry(root, width=55, bd=1, font=('Arial', 15), textvariable = ph3)
mtgradeEntry = Entry(root, width=55, bd=1, font=('Arial', 15), textvariable = ph4)
enggradeEntry = Entry(root, width=55, bd=1, font=('Arial', 15), textvariable = ph5)

studidEntry.grid(row=3, column=1, columnspan=4, padx=5, pady=0)


fnameEntry.grid(row=4, column=1, columnspan=4, padx=5, pady=0)
scigradeEntry.grid(row=5, column=1, columnspan=4, padx=5, pady=0)
mtgradeEntry.grid(row=6, column=1, columnspan=4, padx=5, pady=0)
enggradeEntry.grid(row=7, column=1, columnspan=4, padx=5, pady=0)

#Button size, color and font of the text in buttons


addBtn = Button(
root, text="Add", padx=45, pady=25, width=10,
bd=2, font=('Arial', 15), bg="#F4FE82", command=add)
updateBtn = Button(
root, text="Update", padx=45, pady=25, width=10,
bd=2, font=('Arial', 15), bg="#F4FE82", command=update)
deleteBtn = Button(
root, text="Delete", padx=45, pady=25, width=10,
bd=2, font=('Arial', 15), bg="#F4FE82", command=delete)
searchBtn = Button(
root, text="Search", padx=45, pady=25, width=10,
bd=2, font=('Arial', 15), bg="#F4FE82", command=search)
selectBtn = Button(
root, text="Select", padx=45, pady=25, width=10,
bd=2, font=('Arial', 15), bg="#F4FE82", command=select)
averageBtn = Button(
root, text="Average", padx=45, pady=25, width=10,
bd=2, font=('Arial', 15), bg="#F4FE82", command=average)

#Placing of buttons
addBtn.grid(row=3, column=5, columnspan=1, rowspan=2)
updateBtn.grid(row=5, column=5, columnspan=1, rowspan=2)
deleteBtn.grid(row=7, column=5, columnspan=1, rowspan=2)
searchBtn.grid(row=9, column=5, columnspan=1, rowspan=2)
selectBtn.grid(row=13, column=5, columnspan=1, rowspan=2)
averageBtn.grid(row=15, column=5, columnspan=1, rowspan=2)

#Font style and size heading part of program


style = ttk.Style()
style.configure("Treeview.Heading", font=('Arial Bold', 15))

#Column part of program


my_tree['columns'] = ("Student ID","Full Name","Science","Math","English")

my_tree.column("#0", width=0, stretch=NO)


my_tree.column("Student ID", anchor=W, width=150)
my_tree.column("Full Name", anchor=W, width=150)
my_tree.column("Science", anchor=W, width=150)
my_tree.column("Math", anchor=W, width=150)
my_tree.column("English", anchor=W, width=150)

#Heading part of program


my_tree.heading("Student ID", text="Student ID", anchor=W)
my_tree.heading("Full Name", text="Full Name", anchor=W)
my_tree.heading("Science", text="Science", anchor=W)
my_tree.heading("Math", text="Math", anchor=W)
my_tree.heading("English", text="English", anchor=W)

refreshTable()

#Run the program


root.mainloop()

You might also like