import tkinter as tk
from tkinter import ttk
import mysql.connector
# Setting up the main window
root = tk.Tk()
root.title("Student Management System")
root.geometry("600x400")
# Add a label to the window
label = tk.Label(root, text="Student Management System", font=("Arial", 16))
label.pack(pady=10)
# Create labels and entry fields for student details
tk.Label(root, text="Name:").pack(pady=5)
entry_name = tk.Entry(root)
entry_name.pack(pady=5)
tk.Label(root, text="Roll No:").pack(pady=5)
entry_roll_no = tk.Entry(root)
entry_roll_no.pack(pady=5)
tk.Label(root, text="Class:").pack(pady=5)
entry_class = tk.Entry(root)
entry_class.pack(pady=5)
tk.Label(root, text="Date of Birth:").pack(pady=5)
entry_dob = tk.Entry(root)
entry_dob.pack(pady=5)
tk.Label(root, text="Contact:").pack(pady=5)
entry_contact = tk.Entry(root)
entry_contact.pack(pady=5)
# Create a Treeview widget to display student records
tree = ttk.Treeview(root, columns=("ID", "Name", "Roll No", "Class", "DOB", "Contact"),
show="headings")
tree.pack(pady=20)
# Define columns
tree.heading("ID", text="ID")
tree.heading("Name", text="Name")
tree.heading("Roll No", text="Roll No")
tree.heading("Class", text="Class")
tree.heading("DOB", text="DOB")
tree.heading("Contact", text="Contact")
# Function to insert student into the database
def add_student():
name = entry_name.get()
roll_no = entry_roll_no.get()
student_class = entry_class.get()
dob = entry_dob.get()
contact = entry_contact.get()
# Connect to MySQL and insert data
db = mysql.connector.connect(
host="localhost", user="your_username", password="your_password",
database="student_management" )
cursor = db.cursor()
cursor.execute(
"INSERT INTO students (name, roll_no, class, dob, contact) VALUES (%s, %s, %s, %s, %s)",
(name, roll_no, student_class, dob, contact) )
db.commit()
db.close()
# Clear the fields after adding
entry_name.delete(0, tk.END)
entry_roll_no.delete(0, tk.END)
entry_class.delete(0, tk.END)
entry_dob.delete(0, tk.END)
entry_contact.delete(0, tk.END)
# Refresh the Treeview
show_students()
# Function to fetch and display all students
def show_students():
# Clear existing data in the Treeview
for row in tree.get_children():
tree.delete(row)
# Fetch students from MySQL and display in the Treeview
db = mysql.connector.connect(
host="localhost", user="your_username", password="your_password",
database="student_management")
cursor = db.cursor()
cursor.execute("SELECT * FROM students")
students = cursor.fetchall()
for student in students:
tree.insert("", tk.END, values=student)
db.close()
# Button to add student
btn_add = tk.Button(root, text="Add Student", command=add_student)
btn_add.pack(pady=10)
# Show students when the app starts
show_students()
# Start the Tkinter loop
root.mainloop()