Employee Management System using Python.
Tags: webdev, beginners, programming, python
Published: 2024-04-21 13:57:14

## 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, you're going to explore creating an employee management system using [Python](https://www.python.org/), [Tkinter](https://docs.python.org/3/library/tkinter.html), and [SQLite3](https://docs.python.org/3/library/sqlite3.html).

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 the goal here? Well, building a personalised employee management system using Python's Tkinter for crafting the interface and SQLite3 for managing the database. You'll cover creating an easy-to-use data entry screen, handling data storage and retrieval smoothly, and even giving 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 employee management system that fits your business like a glove.

##Project Setup

When working in [Visual Studio Code](https://code.visualstudio.com/) (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](https://code.visualstudio.com/) and creating a new folder:


###Step 1

Open [VS Code](https://code.visualstudio.com/) 

![Vs Code Home page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9h91xy65zqbuxozdayrs.png)




###Step 2

Create new folder



![New Folder on Vs Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8gmdp57jqiu7pprbc0ug.png)



###Step 3

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



![new file on vs code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzl3uirvda6j5qus2rf4.png)


Before you dive in, you will start by installing and importing the necessary libraries.

###Step 4

Install the necessary library:


<prev>
```python
pip install customtkinter
pip install sqlite3
```
</prev>
###Step 5

Next, Import the necessary libraries:

<prev>
```python
import sqlite3
import customtkinter
from tkinter import messagebox
from tkinter import *
from tkinter import ttk
```
</prev>

**[sqlite3](https://docs.python.org/3/library/sqlite3.html)  - This helps store and handle employee data in a database.**

**[customtkinter](https://pypi.org/project/customtkinter/0.3/) - This is a special toolkit for making the system look and work a certain way.**

**[messagebox](https://docs.python.org/3/library/tkinter.messagebox.html) (from tkinter): This shows messages or notifications to the user.**

**[tkinter](https://docs.python.org/3/library/tkinter.html) - This is the main tool for creating the system's visual interface.**

**[ttk](https://docs.python.org/3/library/tkinter.html) (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

Start by creating a simple window. You'll begin by setting up a variable `app`:


<prev>
```python
app = customtkinter.CTK()
app.title("Employee System")
app.geometry("800x500")
app.config(bg="#17043d")
```
</prev>


###Step 2

Define different fonts that you will use for your buttons and entry boxes. These fonts will help you maintain consistency in the appearance of text displayed on buttons and entry boxes within your application. By setting these fonts, you can easily apply them to different widgets throughout your Employee System interface:


<prev>
```python
font1 = (“Arial”, 20, “bold”)
font2= (“Arial”, 15, “bold”)
font3 = (“Arial”,12, “bold”)
```
</prev>


###Step 3

Add a frame inside the window next:


<prev>
```python
frame1 = customtkinter.CTkFrame(app, fg_color="#FFFFFF")
frame1.place(x=350, y=0, width=450, height=500)
```
</prev>


###Step 4

See how your main window looks:


<prev>
```python
app.mainloop()
```
</prev>


###Step 5

Save and run the code written so far. 

![Main Window of Employee Management System](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r92h2ylqxnv61jexrb28.png)



##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
 
Establish a connection to SQLite database with name: `Employee.db` using the`sqlite3.connect()` method. By creating this table in the database, you'll be able to conveniently store and retrieve employee data for different tasks and operations in your application. This will help you efficiently manage and work with employee information as needed:

<prev>
```python
db = sqlite3.connect("Employee.db")
db.execute("CREATE TABLE IF NOT EXISTS EMPLOYEES (Employee_ID INTEGER, Name TEXT, Age TEXT, Role TEXT)")
```
</prev>

##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, Create and define your labels `ID`,`Name`,`Age`,`Role`.

###Step 1

Create a user input form with a label `ID:` and an entry field:

<prev>
```python
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)
```
</prev>

###Step 2

Next, Create a user input form with a label `Name:` and an entry field:

<prev>
```python
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)
```
</prev>

###Step 3

Create a user input form with the label `Age:` and an entry field:

<prev>
```python
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)
```
</prev>

###Step 4

Finally, create a user input form with the label `Role:` and an entry field:

<prev>
```python
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)
```
</prev>

###Step 5

Save and run the code written so far. 


![Image of our main window with labels,entry boxes and combo box](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j600ilygewyck6dus7bl.png)




##Buttons

In an employee management system, buttons are like tools that help users do things like add, change, or remove employee information.  Well-designed buttons make it simple for users to navigate and control tasks smoothly.
To create the buttons for our windows, you will create and define new variables.


###Step 1

In this step, you will 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:

<prev>
```python
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)
```
</prev>

###Step 2

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

<prev>
```python
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)
```
</prev>

###Step 3

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

<prev>
```python
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)
```
</prev>

###Step 4

In the final step, Define a `Delete` button for the window. The `Delete`button enables the removal of selected employee records from the system:

<prev>
```python
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)
```
</prev>

###Step 5

Save and run the code written so far. 


![Image of our Buttons](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1pzpe0s9wb8vh6n8hvb.png)


##Treeview

When you're making an employee management system with Python, `treeview`is 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, Use the tree view to insert the employee details:

<prev>
```python
style = ttk.Style()
```
</prev>
###Step 2

Next, modify the body font:

<prev>
```python
style.configure("mystyle.Treeview", font=font3, rowheight=50)
```
</prev>
###Step 3

Modify the heading font:

<prev>
```python
style.configure("mystyle.Treeview.Heading", font=font2)
```
</prev>
###Step 4

Here, remove the `treeview` border:

<prev>
```python
style.layout("mystyle.Treeview", [("mystyle.Treeview.treearea", {"sticky": "nswe"})])
```
</prev>

###Step 5

Save and run the code written so far. 


![Image of our Employee Management System treeview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eucb76555eagd2l790yy.png)



##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

Create your `treeview` columns and headings:

<prev>
```python
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)
```
</prev>

###Step 2

Display the heading of our tree view :

<prev>
```python
tv.pack()
```
</prev>

###Step 3

Create your functions to insert your data into the Treeview:

<prev>
```python
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.")
```
</prev>

###Step 4

Create another function to clear all  entry boxes:

<prev>
```python
def clear():
    id_entry.delete(0, END)
    name_entry.delete(0, END)
    age_entry.delete(0, END)
    role_entry.delete(0, END)
```
</prev>

###Step 5

Add the insert function and clear function in `save_button` and `clear_button `

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

###Step 6

Finally, to view every employee's details in the database on your treeview, create new functions `fetch` and `display_data`:

<prev>
```python
def fetch():
    cursor.execute("SELECT * FROM EMPLOYEE")
    rows = cursor.fetchall()
    return rows
```
</prev>

<prev>
```python
def display_data():
    tv.delete(*tv.get_children())
    for row in fetch():
        tv.insert("", END, values=row)
```
</prev>

`fetch`: is responsible for getting every employee from your database.

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


###Step 7

Finally, view your `display_data` in our tv:

<prev>
```python
display_data()
```
</prev>

###Step 8

Save the code 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

Add the `display_data` function to your `insert function`:

<prev>
```python
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()
```
</prev>

###Step 2

Save and run the code.


![Image of our employee management system with new employee data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8b5b6smb3a2lem1ko3h.png)





##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 allows you to remove a specific employee's profile from your system entirely and keeps your employee list accurate and up-to-date.

###Step 1

Delete an entry from your database, and to do this, create a new function `delete`:

<prev>
```python
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()
```
</prev>

###Step 2

Give the `delete_button` a `delete command`.

**delete_button.config(command=delete)**

###Step 3

Save the code written so far. Once it's saved, delete employee ID: 150 to see if it works as expected.


![Image of our employee management system with employee ID :150 ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuxdp2zhb3b75gm69ot9.png)



![Image of our employee management system without employee ID :150 ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xo19oibwefh0o2t730v.png)



##Update functions
 
So, 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

Create a new function called `get_data` :

<prev>
```python
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])
```
</prev>

###Step 2

Call the function `get_data`:

<prev>
```python
tv.bind("<ButtonRelease-1>", get_data)
```
</prev>

###Step 3

Create a new function `update` and create a new variable inside it :

<prev>
```python
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()
```
</prev>

###Step 4

Parse the `update_button` using the `update` function with the command set as `update.`

**update_button.config(command=update)**

###Step 5

Save and run code.


![final Image of employee management system](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bb71qxkfuzszd1y876at.png)




##Conclusion

In this article, you learned about making an employee management system using Python. This system helps companies organize employee information easily and accurately. You started by getting everything set up for the project, connecting to a database, and creating functions to add, read, update, and delete employee data. You used Tkinter to create a simple interface with fields for entering data and buttons for performing actions.
 
You added a feature to display employee details in a structured way using the tree view. This enables you to see all the employee information at a glance. You could also add new employees, delete existing ones, and update their details as needed. Using Python's flexibility and ease of use, you built 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