DEV Community

Cover image for Employee Management System using Python.
Ebikara Dandeson Spiff
Ebikara Dandeson Spiff

Posted on

Employee Management System using Python.

Introduction

Dealing with piles of papers or scattered Excel sheets for employee information can be a real headache, right? Well, what if I told you there's a smoother way to handle all that? A system that lets you easily store, update, and find details about your employees in just a few clicks. Sounds neat, doesn't it? In this article, we're going to explore creating an employee management system using Python, Tkinter, and SQLite3.

Now, employee management systems have evolved into must-haves for businesses to keep their workforce in check. You can start from the basics, like employee names and contacts, all the way to tracking their attendance and performance evaluations. By automating these tasks, you not only save time but also minimize errors, making it a win-win situation for everyone involved.

So, what's our goal here? Well, we'll walk you through building a personalised employee management system using Python's Tkinter for crafting the interface and SQLite3 for managing the database. We'll cover creating an easy-to-use data entry screen, handling data storage and retrieval smoothly, and even giving you hints on how to level up the system according to your company's needs. By the time you finish reading, you'll be all set to craft your very own employee management system that fits your business like a glove.

Project Setup

When working in Visual Studio Code (VS Code), always create a new Python file for your project.

It's helpful to have separate files for different parts of your project.

To do this, you can start by opening your VS Code and creating a new folder:

Step 1

Open VS Code

Vs Code Home page

Step 2

Create new folder

New Folder on Vs Code

Step 3

Head over to the newly created folder and create a new file app.py.

new file on vs code

Before we dive in, let's start by installing and importing the necessary libraries.

Step 4

Let's install the necessary library:

pip install customtkinter
pip install sqlite3
Enter fullscreen mode Exit fullscreen mode

Step 5

Next, we will Import the necessary libraries:

import sqlite3
import customtkinter
from tkinter import messagebox
from tkinter import *
from tkinter import ttk
Enter fullscreen mode Exit fullscreen mode

sqlite3 - This helps store and handle employee data in a database.

customtkinter - This is a special toolkit for making the system look and work a certain way.

messagebox (from tkinter): This shows messages or notifications to the user.

tkinter - This is the main tool for creating the system's visual interface.

ttk (from tkinter) -This helps make the system's design look more modern and consistent across different computers.

When you use the asterisk (*) symbol in the import statement in Python, such as from tkinter import * it brings all the names from the module directly into your code. This can make it easier to access things.

Database Connection & Function

When you're creating an employee management system with Python, Database Connection & Functions basically means hooking up a database to your system and making sure it can do everything you need it to do with the employee information. So, it's like setting up a connection between your Python code and the database and then adding in functions to add, update, and delete employee records. It's all about making sure your system can handle employee data smoothly and efficiently.

Step 1

Let's start by creating a simple window. We'll begin by setting up a variable app:

app = customtkinter.CTK()
app.title("Employee System")
app.geometry("800x500")
app.config(bg="#17043d")
Enter fullscreen mode Exit fullscreen mode

Step 2

Let's define different fonts that we will use for our buttons and entry boxes. These fonts will help us maintain consistency in the appearance of text displayed on buttons and entry boxes within our application. By setting these fonts, we can easily apply them to different widgets throughout our Employee System interface:

font1 = (Arial, 20, bold)
font2= (Arial, 15, bold)
font3 = (Arial,12, bold)
Enter fullscreen mode Exit fullscreen mode

Step 3

Okay, so the next step is to add a frame inside the window:

frame1 = customtkinter.CTkFrame(app, fg_color="#FFFFFF")
frame1.place(x=350, y=0, width=450, height=500)
Enter fullscreen mode Exit fullscreen mode

Step 4

Now, to see how our main window looks:

app.mainloop()
Enter fullscreen mode Exit fullscreen mode

Step 5

Let's go ahead to save and run the code we've written so far.

Main Window of Employee Management System

Setting up an Employee Database in SQLite

When setting up an employee database, you're essentially creating a well-organized system where you can easily access and manage information about your employees. Think of it as having everything neatly arranged on digital shelves. Each employee gets their spot in the database with all the key details, like their name, job role, department, and contact information, laid out in a structured way. This makes it super convenient to search for particular details when you need them.

Step 1

Let's establish a connection to our SQLite database named Employee.db using thesqlite3.connect() method. By creating this table in the database, we'll be able to conveniently store and retrieve employee data for different tasks and operations in our application. This will help us efficiently manage and work with employee information as needed:

db = sqlite3.connect("Employee.db")
db.execute("CREATE TABLE IF NOT EXISTS EMPLOYEES (Employee_ID INTEGER, Name TEXT, Age TEXT, Role TEXT)")
Enter fullscreen mode Exit fullscreen mode

Labels & Entry Boxes & Combo Box

When creating an employee management system with Python, labels, entry boxes, and combo boxes are essential for building a user-friendly interface that guides users through the process. Labels help by showing text or instructions; entry boxes allow users to input data such as employee details; and combo boxes simplify the selection of options from a list. By combining these elements, the system becomes more intuitive and enhances the overall user experience. So, including these GUI components is crucial for effectively managing employee data and ensuring smooth user interaction.

Here, we want to create and define our labels ID,Name,Age,Role.

Step 1

Let's create a user input form with a label ID: and an entry field:

id_label = customtkinter.CTkLabel(app, text="ID:", text_font=font1)
id_label.place(x=20, y=20)
id_entry = customtkinter.CTkEntry(app, text_font=font2, text_color="#000000", fg_color="#FFFFFF", border_color="#FFFFFF", width=200)
id_entry.place(x=140, y=20)
Enter fullscreen mode Exit fullscreen mode

Step 2

Next, we create a user input form with a label Name: and an entry field:

name_label = customtkinter.CTkLabel(app, text="Name:", text_font=font1)
name_label.place(x=20, y=80)
name_entry = customtkinter.CTkEntry(app, text_font=font2, text_color="#000000", fg_color="#FFFFFF", border_color="#FFFFFF", width=200)
name_entry.place(x=140, y=80)
Enter fullscreen mode Exit fullscreen mode

Step 3

Then we create a user input form with the label Age: and an entry field:

age_label = customtkinter.CTkLabel(app, text="Age:", text_font=font1)
age_label.place(x=20, y=140)
age_entry = customtkinter.CTkEntry(app, text_font=font2, text_color="#000000", fg_color="#FFFFFF", border_color="#FFFFFF", width=200)
age_entry.place(x=140, y=140)
Enter fullscreen mode Exit fullscreen mode

Step 4

Lastly, let's create a user input form with the label Role: and an entry field:

role_label = customtkinter.CTkLabel(app, text="Role:", text_font=font1)
role_label.place(x=20, y=200)
role_entry = customtkinter.CTkEntry(app, text_font=font2, text_color="#000000", fg_color="#FFFFFF", border_color="#FFFFFF", width=200)
role_entry.place(x=140, y=200)
Enter fullscreen mode Exit fullscreen mode

Step 5

Let's save and run the code we've written so far.

Image of our main window with labels,entry boxes and combo box

Buttons

In an employee management system, buttons are like tools that help users do things like add, change, or remove employee information. They work like a steering wheel in a car, making it easier for users to move around and get things done in the system. Well-designed buttons make it simple for users to navigate and control tasks smoothly.
Now, to create the buttons for our windows, we will create and define new variables.

Step 1

In this step, let's define and create a Save button for the Employee Management System window. The Save button is crucial for adding new employee information to the system. It allows users to save the data entered in the input fields:

save_button = customtkinter.CTkButton(app, command=insert, text="Save", text_font=font1, fg_color="#03a819", hover_color="#03a819", corner_radius=20, width=120, cursor="hand2")
save_button.place(x=70, y=250)
Enter fullscreen mode Exit fullscreen mode

Step 2

Next, we will define and create an Update button for the window. The Update button facilitates the modification of existing employee records within the system:

update_button = customtkinter.CTkButton(app, command=update, text="Update", text_font=font1, fg_color="#b86512", hover_color="#b86512", corner_radius=20, width=127, cursor="hand2")
update_button.place(x=200, y=250)
Enter fullscreen mode Exit fullscreen mode

Step 3

Here, let's create a Clear button for the window. The Clear button offers users a way to remove input data or reset the form fields:

clear_button = customtkinter.CTkButton(app, command=clear, text="Clear", text_font=font1, fg_color="#6e0e53", hover_color="#6e0e53", corner_radius=20, width=120, cursor="hand2")
clear_button.place(x=70, y=300)
Enter fullscreen mode Exit fullscreen mode

Step 4

In the final step, we are going to define a Delete button for the window. The Deletebutton enables the removal of selected employee records from the system:

delete_button = customtkinter.CTkButton(app, command=delete, text="Delete", text_font=font1, fg_color="#cf061a", hover_color="#cf061a", corner_radius=20, width=140, cursor="hand2")
delete_button.place(x=200, y=300)
Enter fullscreen mode Exit fullscreen mode

Step 5

Let's go ahead to save and run the code we've written so far.

Image of our Buttons

Treeview

When you're making an employee management system with Python, treeviewis like a digital tree that helps organise your employee information. It sorts them into groups by things like departments and shows them in a neat list. It's a helpful way to keep track of who's on your team.

Step 1

First, We will use the tree view to insert the employee details:

style = ttk.Style()
Enter fullscreen mode Exit fullscreen mode

Step 2

Next, we want to modify the body font:

style.configure("mystyle.Treeview", font=font3, rowheight=50)
Enter fullscreen mode Exit fullscreen mode

Step 3

Now, let's modify the heading font:

style.configure("mystyle.Treeview.Heading", font=font2)
Enter fullscreen mode Exit fullscreen mode

Step 4

Here, we are removing the treeview border:

style.layout("mystyle.Treeview", [("mystyle.Treeview.treearea", {"sticky": "nswe"})])
Enter fullscreen mode Exit fullscreen mode

Step 5

Let's save and run the code we've written so far.

Image of our Employee Management System treeview

Insert Data to Tree View

When you're building an employee management system with Python, Insert Data to TreeView simply means adding new employee information to the organised list displayed in the tree view.

Step 1

To start, we will create our treeview columns and headings:

tv.heading("1", text="ID")
tv.column("1", width=105)

tv.heading("2", text="Name")
tv.column("2", width=105)

tv.heading("3", text="Age")
tv.column("3", width=105)

tv.heading("4", text="Role")
tv.column("4", width=105)
Enter fullscreen mode Exit fullscreen mode

Step 2

Then, we display the heading of our tree view :

tv.pack()
Enter fullscreen mode Exit fullscreen mode

Step 3

Next, we will create our functions to insert our data into the Treeview:

def insert():
    if id_entry.get() == "" or name_entry.get() == "" or age_entry.get() == "" or role_entry.get() == "":
        messagebox.showerror(title="Error", message="Please Enter All The Data.")
    else:
        details = [int(id_entry.get()), name_entry.get(), age_entry.get(), role_entry.get()]
        cursor.execute("INSERT INTO Employee VALUES (?,?,?,?)", details)
        db.commit()
        messagebox.showinfo(title="Inserted", message="Employee Has Been Inserted.")
Enter fullscreen mode Exit fullscreen mode

Step 4

Now, we create another function to clear all entry boxes:

def clear():
    id_entry.delete(0, END)
    name_entry.delete(0, END)
    age_entry.delete(0, END)
    role_entry.delete(0, END)
Enter fullscreen mode Exit fullscreen mode

Step 5

After that, we will add the insert function and clear function in our save_button and clear_button

save_button.config(command=insert)
clear_button.config(command=clear)

Step 6

Finally, we want to view every employee's details in the database on our treeview, to do this we will create new functions fetch and display_data:

def fetch():
    cursor.execute("SELECT * FROM EMPLOYEE")
    rows = cursor.fetchall()
    return rows
Enter fullscreen mode Exit fullscreen mode
def display_data():
    tv.delete(*tv.get_children())
    for row in fetch():
        tv.insert("", END, values=row)
Enter fullscreen mode Exit fullscreen mode

fetch: is responsible for getting every employee from our database.

display_data: is responsible for inserting our data in our tv.

Step 7

Finally, let's see our display_data in our tv:

display_data()
Enter fullscreen mode Exit fullscreen mode

Step 8

Save the code we've written.

New Employee Function

Think of the New Employee function as a tool for adding a new employee to your team when you're making an employee management system with Python. It's a way to create a profile for the new team member, where you can put in their name, job title, and other important information. It's like filling out a form to officially bring someone on board. So, it's just a simple way to keep track of who's on your team.

Step 1

To do this let's add the display_data function to our insert function:

def insert():
    if id_entry.get() == "" or name_entry.get() == "" or age_entry.get() == "" or role_entry.get() == "":
        messagebox.showerror(title="Error", message="Please Enter All The Data.")
    else:
        details=[int(id_entry.get()), name_entry.get(), age_entry.get(), role_entry.get()]
        cursor.execute("INSERT INTO Employee VALUES (?,?,?,?)", details)
        db.commit()
        messagebox.showinfo(title="Inserted", message="Employee Has Been Inserted.")
        display_data()
Enter fullscreen mode Exit fullscreen mode

Step 2

Let's go ahead to save and run the code.

Image of our employee management system with new employee data

Delete function

Okay, so imagine you have your employee list all set up in your system, right? But then, someone leaves the company. You don't want their information hanging around anymore. That's where the delete function comes in handy.
It lets you remove a specific employee's profile from your system entirely and keeps your employee list accurate and up-to-date.

Step 1

First, let’s delete an entry from our database, and to do this we will create a new function delete:

def delete():
    cursor.execute("DELETE FROM Employee WHERE Employee_ID=?", [id_entry.get()])
    db.commit()
    messagebox.showinfo(title="Delete", message="Employee Has Been Deleted.")
    display_data()
    clear()
Enter fullscreen mode Exit fullscreen mode

Step 2

Next, we return to the delete_button and give it a delete command.

delete_button.config(command=delete)

Step 3

Let's go ahead and save the code we've written so far. Once it's saved, let's delete employee ID: 150 to see if it works as expected.

Image of our employee management system with employee ID :150

Image of our employee management system without employee ID :150

Update functions

So, let's say someone on your team gets a promotion, or maybe they move to a different department, or their contact information changes. You don't want to delete their whole profile and start over, right? That's where the update function comes in.

It's like having a little edit button for each employee's profile. You can use it to change any outdated information and make sure everything is current.

Step 1

Now, we want to update our employee’s data whenever necessary.
To do this, we will create a new function called get_data :

def get_data(event):
   clear()
   selected_row = tv.focus()
   data = tv.item(selected_row)
   row = data["values"]
   id_entry.insert(0, row[0])
   name_entry.insert(0, row[1])
   age_entry.insert(0, row[2])
   role_entry.insert(0, row[3])
Enter fullscreen mode Exit fullscreen mode

Step 2

Then we will call the functionget_data:

tv.bind("<ButtonRelease-1>", get_data)
Enter fullscreen mode Exit fullscreen mode

Step 3

Next, we will create a new function updateand also create a new variable inside it :

def update():
    new_details = [name_entry.get(), age_entry.get(), role_entry.get(), int(id_entry.get())]
    cursor.execute("UPDATE Employee SET Name=?, Age=?, Role=? WHERE Employee_ID=?", new_details)
    db.commit()
    messagebox.showinfo(title="UPDATED", message="Employee's details have been updated.")
    display_data()
Enter fullscreen mode Exit fullscreen mode

Step 4

Let's go ahead and parse the update_button using the update function with the command set as update.

update_button.config(command=update)

Step 5

Finally, we will save and run our code.

final Image of employee management system

Conclusion

In this article, we learned about making an employee management system using Python. This system helps companies organize employee information easily and accurately. We started by getting everything set up for the project, connecting to a database, and creating functions to add, read, update, and delete employee data. We used Tkinter to create a simple interface with fields for entering data and buttons for performing actions.

We added a feature to display employee details in a structured way using the tree view. This lets us see all the employee information at a glance. We could also add new employees, delete existing ones, and update their details as needed. Using Python's flexibility and ease of use, we showed how to build a practical employee management system that can be customized for different company requirements.

Python's simplicity makes it a good choice for developing efficient and user-friendly management systems. By using Python for such systems, companies can improve efficiency, simplify operations, and make sure employee data is managed effectively. Python's flexibility and user-friendliness make it a valuable tool for

Top comments (2)

Collapse
 
sreno77 profile image
Scott Reno

Very cool project

Collapse
 
spiff profile image
Ebikara Dandeson Spiff

Thank you.