I'll create a simple Employee Management System using Tkinter and SQLite. This system will include:
✅ Add Employee
✅ View Employee List
✅ Update Employee
✅ Delete Employee
✅ Search EmployeeI'll provide step-by-step explanations along with the full Python code.
📌 Step 1: Install Required Libraries
Ensure you have Tkinter (comes with Python) and SQLite (built-in with Python).
If you don't have Pillow for image handling, install it using:
pip install pillow
📌 Step 2: Create the Main Tkinter Window
We will create a GUI where users can enter employee details and manage records.
Here's the full code:
import sqlite3
from tkinter import *
from tkinter import ttk, messagebox
# Create main application window
root = Tk()
root.title("Employee Management System")
root.geometry("800x500")
root.config(bg="#f4f4f4")
# Connect to SQLite Database
conn = sqlite3.connect("employees.db")
cursor = conn.cursor()
# Create Employee Table
cursor.execute("""
CREATE TABLE IF NOT EXISTS employees (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER NOT NULL,
department TEXT NOT NULL,
salary REAL NOT NULL
)
""")
conn.commit()
# Function to Add Employee
def add_employee():
if name_var.get() == "" or age_var.get() == "" or dept_var.get() == "" or salary_var.get() == "":
messagebox.showerror("Error", "All fields are required")
else:
cursor.execute("INSERT INTO employees (name, age, department, salary) VALUES (?, ?, ?, ?)",
(name_var.get(), age_var.get(), dept_var.get(), salary_var.get()))
conn.commit()
messagebox.showinfo("Success", "Employee Added Successfully")
clear_fields()
display_employees()
# Function to Display Employees in Treeview
def display_employees():
for row in employee_table.get_children():
employee_table.delete(row)
cursor.execute("SELECT * FROM employees")
rows = cursor.fetchall()
for row in rows:
employee_table.insert("", END, values=row)
# Function to Get Selected Employee Data
def get_selected(event):
selected_row = employee_table.focus()
data = employee_table.item(selected_row)
row = data["values"]
if row:
id_var.set(row[0])
name_var.set(row[1])
age_var.set(row[2])
dept_var.set(row[3])
salary_var.set(row[4])
# Function to Update Employee
def update_employee():
if id_var.get() == "":
messagebox.showerror("Error", "Select an employee to update")
else:
cursor.execute("UPDATE employees SET name=?, age=?, department=?, salary=? WHERE id=?",
(name_var.get(), age_var.get(), dept_var.get(), salary_var.get(), id_var.get()))
conn.commit()
messagebox.showinfo("Success", "Employee Updated Successfully")
clear_fields()
display_employees()
# Function to Delete Employee
def delete_employee():
if id_var.get() == "":
messagebox.showerror("Error", "Select an employee to delete")
else:
cursor.execute("DELETE FROM employees WHERE id=?", (id_var.get(),))
conn.commit()
messagebox.showinfo("Success", "Employee Deleted Successfully")
clear_fields()
display_employees()
# Function to Search Employee
def search_employee():
search_query = search_var.get()
for row in employee_table.get_children():
employee_table.delete(row)
cursor.execute("SELECT * FROM employees WHERE name LIKE ?", ('%' + search_query + '%',))
rows = cursor.fetchall()
for row in rows:
employee_table.insert("", END, values=row)
# Function to Clear Fields
def clear_fields():
id_var.set("")
name_var.set("")
age_var.set("")
dept_var.set("")
salary_var.set("")
# Variables for Employee Fields
id_var = StringVar()
name_var = StringVar()
age_var = StringVar()
dept_var = StringVar()
salary_var = StringVar()
search_var = StringVar()
# UI Components
Label(root, text="Employee Management System", font=("Arial", 20, "bold"), bg="#f4f4f4").pack()
frame = Frame(root, bg="#f4f4f4")
frame.pack(pady=20)
Label(frame, text="Name:", font=("Arial", 12)).grid(row=0, column=0, padx=10, pady=5, sticky="w")
Entry(frame, textvariable=name_var, width=25).grid(row=0, column=1, padx=10, pady=5)
Label(frame, text="Age:", font=("Arial", 12)).grid(row=1, column=0, padx=10, pady=5, sticky="w")
Entry(frame, textvariable=age_var, width=25).grid(row=1, column=1, padx=10, pady=5)
Label(frame, text="Department:", font=("Arial", 12)).grid(row=2, column=0, padx=10, pady=5, sticky="w")
Entry(frame, textvariable=dept_var, width=25).grid(row=2, column=1, padx=10, pady=5)
Label(frame, text="Salary:", font=("Arial", 12)).grid(row=3, column=0, padx=10, pady=5, sticky="w")
Entry(frame, textvariable=salary_var, width=25).grid(row=3, column=1, padx=10, pady=5)
# Buttons
btn_frame = Frame(root, bg="#f4f4f4")
btn_frame.pack()
Button(btn_frame, text="Add", command=add_employee, width=10, bg="green", fg="white").grid(row=0, column=0, padx=5, pady=10)
Button(btn_frame, text="Update", command=update_employee, width=10, bg="blue", fg="white").grid(row=0, column=1, padx=5, pady=10)
Button(btn_frame, text="Delete", command=delete_employee, width=10, bg="red", fg="white").grid(row=0, column=2, padx=5, pady=10)
Button(btn_frame, text="Clear", command=clear_fields, width=10, bg="gray", fg="white").grid(row=0, column=3, padx=5, pady=10)
# Search Box
search_frame = Frame(root, bg="#f4f4f4")
search_frame.pack()
Entry(search_frame, textvariable=search_var, width=30).grid(row=0, column=0, padx=10, pady=10)
Button(search_frame, text="Search", command=search_employee, bg="orange", fg="white").grid(row=0, column=1, padx=5)
# Employee Table
columns = ("ID", "Name", "Age", "Department", "Salary")
employee_table = ttk.Treeview(root, columns=columns, show="headings")
employee_table.heading("ID", text="ID")
employee_table.heading("Name", text="Name")
employee_table.heading("Age", text="Age")
employee_table.heading("Department", text="Department")
employee_table.heading("Salary", text="Salary")
employee_table.bind("<ButtonRelease-1>", get_selected)
employee_table.pack(fill=BOTH, expand=True)
# Load Employees
display_employees()
root.mainloop()
🔹 Features of This Program
✅ SQLite Database for data storage
✅ Tkinter GUI for managing employees
✅ CRUD Operations (Add, Update, Delete, View)
✅ Search Functionality
✅ Interactive Table for employee records🔹 How to Run This Program
1️⃣ Copy and save the above code as employee_manager.py.
2️⃣ Run it using:python employee_manager.py
3️⃣ The GUI will open, allowing you to add, update, delete, and search employees.
Would you like me to add more features like exporting to CSV, sorting, or filtering by department? 🚀
0 Comments