How to Generate Payslip using Python Tkinter + Video Tutorial

In this Python tutorial, you will learn how to create a payslip generator using Python console and Graphical User Interface (GUI). By the end of this blog, you will have complete knowledge and the source code of creating a payslip generator in python using Python Tkinter.

Business Requirement

Recently, we received a requirement to create a payslip application in python. It’s a simple console application that accepts inputs like employee name, working hours, hourly rate, etc.

Using this information the application generates an output wherein employee information and deductions are displayed on the python console.

Though the requirement of an application was console-based we have created it using both console and GUI-based. For graphical user interface (GUI) we have used the python library – Tkinter.

If you are new to Python Tkinter, check out Python GUI Programming (Python Tkinter)

Below is the image of the requirement that we received:

How to Generate Payslip using Python Tkinter
payslip console application
  • Simple Payslip application using python console
  • Simple payslip application using python tkinter

Also, check out, How to convert Python file to exe using Pyinstaller

Simple Payslip application using python console

Payslip application accepts employee details like name, working hours, rate per hour, sss contribution, Phil health, and housing loan.

Using this information, a payslip is generated that has employee information with all the deductions and in the end, net salary is displayed.

Source Code:

In the below source code, the python input() method is used to take user inputs. Input provided by the user is by default in string data type.

  • By adding data type name before input() method we can convert the user input into data type of preference. Like in the below code we converted user inputs to float data type.
  • Number of days duty server times rate per hour will produce gross salary of an employee.
  • Tax is 10 percent of gross salary
  • Adding up of SSS contribution, phil health, house loan and tax will give the total deduction
  • Subtract total dedcution from gross salary to get the net salary information.
# User inputs
emp_name = input('Employee Name: ')
rph = float(input('Rate per hour: '))
duty = float(input('Duty (No. of hours): '))
s3_contrib = float(input('SSS Contribution: '))
phealth = float(input('PhilHealth: '))
hloan = float(input('Housing loan: '))

print('')
print('-'*3+'EMPLOYEE INFORMATION'+'-'*3)
print('')

# Net salary calculation
gross_sal = rph * duty
tax = gross_sal * 0.1
total_deduction = s3_contrib + phealth + hloan + tax
net_sal = gross_sal - total_deduction


# display information using formatted string
print(f'''
Name: {emp_name}
Rendered Hours: {duty}
Rate per hour: {rph}
Gross Salary: {gross_sal}
''')

print('')
print('-'*5+'DEDUCTIONS'+'-'*5)
print('')

print(f'''
SSS: {s3_contrib}
PhilHealth: {phealth}
Other Loan: {hloan}
Tax: {tax}
Total Deductions: {total_deduction}

Net Salary: {net_sal}
''')

Output:

READ:  Attributeerror module 'tensorflow' has no attribute 'summary'

In the below output, the user has provided the first 6 inputs rest of everything is generated by the application. The information is divided into 2 sections – Employee information and Deductions. and in the end, the net salary in hand is calculated.

payslip console application using python
payslip console application using python

Read Create Word Document in Python Tkinter

Simple payslip application using python tkinter

Python Tkinter allows python developers to write GUI-based software. The wide variety of widgets allows creating a user-friendly interface that allows to access the application easily.

In this section, we have created a GUI-based application that accepts user inputs through entry widgets and displays results in an interactive window. The program is written in python using the Tkinter module.

The code is divided into two parts. In the first part, we have created an interactive form using Python Tkinter. Fill in the interactive form and click on the calculate button to generate a payslip. The other part displays the output in the new window.

Source Code:

In the below code, the Tkinter module is imported, calsal module is created by us, and will discuss it in the next part.

  • Save this code withe name main.py
  • ws is the instance created for tkinter object in python.
  • Clear function clears all the entry widgets when clear button is pressed. <entry_widget_name>.delete(0, 'end'). Here, 0 shows starting point and end shows the last character inside the python tkinter entry widget.
  • Exit button will terminate all the instances of the program. Using anonymous function (lambda) we have created exit function in one line – lambda: ws.destroy() here, ws is the name of current window instance.
  • Python function name – Main() holds the algorithm that will generate payslip details in tkinter based GUI.
  • Below formula is used to calculate the net salary of an employee:
gross_sal = r * d
tax = gross_sal * 0.1
total_deduction = contri + ph + hl + tax
net_sal = gross_sal - total_deduction
  • Grid geomertry method is used to place widgets on the tkinter window. Learn more about tkinter grid geometry method.
  • Entry widgets are used to take user input. String is the default data type of Entry widget. Using <entry>.get() method, information from entry widget can be fetched. Learn more about Entry widgets in python tkinter.
calsal.payslip(ename, d, r, gross_sal, contri, ph, hl, tax, total_deduction, net_sal, ws, f)
  • The above code shows calling of calsal function. We have created a function payslip in this module. Payslip function accepts above mentioned parameters.
  • This function is responsible for generating interactive output window.
# modules
from tkinter import *
import calsal

# tkinter object 
ws = Tk()
ws.title('PythonGuides - PaySlip Generator')
ws.geometry('400x300')

f = 'sans-serif, 12'

# functions
def clearbtn():
    emp_name.delete(0, 'end')
    rph.delete(0, 'end')
    duty.delete(0, 'end')
    s3_contrib.delete(0, 'end')
    phealth.delete(0, 'end')
    hloan.delete(0, 'end')


def main():
    ename = emp_name.get()
    r = rph.get()
    d = duty.get()
    contri = s3_contrib.get()
    ph = phealth.get()
    hl = hloan.get()

    r = float(r)
    d = float(d) 
    contri = float(contri)
    ph = float(ph)
    hl = float(hl)

    gross_sal = r * d
    tax = gross_sal * 0.1
    total_deduction = contri + ph + hl + tax
    net_sal = gross_sal - total_deduction
    
    # calling function
    calsal.payslip(ename, d, r, gross_sal, contri, ph, hl, tax, total_deduction, net_sal, ws, f)
    
Label(
    ws,
    text="EMPLOYEE PAYSLIP",
    font=('sans-serif, 14'),
    relief=SOLID,
    padx=10,
    pady=5
).pack()

# frame widget
mainframe = Frame(ws, padx=5, pady=5)
mainframe.pack(expand=True)

# label widget
Label(
    mainframe,
    text='Employee Name',
    font=f
    ).grid(row=2, column=0, sticky='e')

Label(
    mainframe,
    text='Rate per hour',
    font=f
    ).grid(row=3, column=0, sticky='e')

Label(
    mainframe,
    text='Duty (No. of hours)',
    font=f
    ).grid(row=4, column=0, sticky='e')

Label(
    mainframe,
    text='SSS Contribution',
    font=f
    ).grid(row=5, column=0, sticky='e')

Label(
    mainframe,
    text='PhilHealth',
    font=f
    ).grid(row=6, column=0, sticky='e')

Label(
    mainframe,
    text='House loan',
    font=f
    ).grid(row=7, column=0, sticky='e')


# Entry widgets
emp_name = Entry(mainframe, font=f)
rph = Entry(mainframe, font=f)
duty = Entry(mainframe, font=f)
s3_contrib = Entry(mainframe, font=f)
phealth = Entry(mainframe, font=f)
hloan = Entry(mainframe, font=f)

# geometry method - Grid
emp_name.grid(row=2, column=1, padx=5)
rph.grid(row=3, column=1, padx=5, sticky='w')
duty.grid(row=4, column=1, padx=5, sticky='w')
s3_contrib.grid(row=5, column=1, padx=5, sticky='w')
phealth.grid(row=6, column=1, padx=5, sticky='w')
hloan.grid(row=7, column=1, padx=5, sticky='w')

# default values in the entry widget
emp_name.insert('0', 'Noel B. Atazar')
rph.insert('0', 150)
duty.insert('0', 9)
s3_contrib.insert('0', 200)
phealth.insert('0', 150)
hloan.insert('0', 100)

# frame for buttons
frame = Frame(mainframe)
frame.grid(row=8, columnspan=3, pady=(30, 0))

# button widget
Button(
    frame,
    text='Calculate',
    width=10,
    command=main,
    font=f,
    bg='#91BF2C'
).pack(side=LEFT, expand=True, padx=(0, 5))

Button(
    frame,
    text='Clear',
    width=5,
    font=f,
    bg='#E6D92A',
    command=clearbtn
).pack(side=LEFT, expand=True, padx=(0, 5))

Button(
    frame,
    text='Exit',
    width=5, 
    font=f,
    bg='#FF614F',
    command=lambda:ws.destroy()
).pack(side=LEFT, expand=True, padx=(0, 5))

# infinite loop
ws.mainloop()

Output:

READ:  Matplotlib scatter plot color

Here is the output of the above-mentioned code, This is an interactive form, once the requirement is filled click on the calculate button to produce the payslip. The clear button will clear the entry boxes and the exit button will terminate the program.

Pay slip GUI application using Python Tkinter
Payslip GUI application using Python Tkinter

Source Code:

Save this file with the name calsal.py. The main.py file will use this module to display the output.

# module
from tkinter import *

# function
def payslip(ename, d, r, gross_sal, contri, ph, hl, tax, total_deduction, net_sal, ws, f):
    cf = ('sans-serif 12 bold')
    win = Toplevel(ws)
    win.geometry('500x450+500+200')  
    
    Label(
        win, 
        text='EMPLOYEES INFORMATION', 
        font='sans-serif 14', 
        relief=SOLID, padx=5, 
        pady=10
        ).place(x=110 , y=10)

    Label(
        win, 
        text='Name: ', 
        font=cf
        ).place(x=10, y=70)

    Label(
        win, 
        text=f'{ename}', 
        font=f
        ).place(x=65, y=70)

    Label(
        win, 
        text='Rendered Hours: ', 
        font=cf
        ).place(x=250, y=70)

    Label(
        win, 
        text=f'{d}', 
        font=f
        ).place(x=390, y=70)

    Label(
        win, 
        text='Rate per hour: ', 
        font=cf
        ).place(x=10, y=110)

    Label(
        win, 
        text=f'{r}', 
        font=f
        ).place(x=125, y=110)

    Label(
        win, 
        text='Gross Salary: ',
        font=cf
        ).place(x=250, y=110)

    Label(
        win, 
        text=f'{gross_sal}', 
        font=f
        ).place(x=390, y=110)


    Label(
        win, 
        text='DEDUCTIONS',
        font='sans-serif 14', relief=SOLID, 
        pady=5, padx=10
        ).place(x=170, y=180)


    Label(
        win, 
        text='SSS: ', 
        font=cf
        ).place(x=10, y=240)

    Label(
        win, 
        text=f'{contri}', 
        font=f
        ).place(x=65, y=240)

    Label(
        win, 
        text='PhilHealth: ', 
        font=cf
        ).place(x=250, y=240)

    Label(
        win, 
        text=f'{ph}', 
        font=f
        ).place(x=390, y=240)

    Label(
        win, 
        text='Other Loan:', 
        font=cf
        ).place(x=10, y=280)
    
    Label(
        win,
        text=f'{hl}', 
        font=f
        ).place(x=105, y=280)

    Label(
        win, 
        text='Tax: ', 
        font=cf
        ).place(x=250, y=280)

    Label(
        win, 
        text=f'{tax}', 
        font=f
        ).place(x=390, y=280)

    Label(
        win, 
        text='Total Deductions: ', 
        font=cf
        ).place(x=10, y=320)

    Label(
        win, 
        text=f'{total_deduction}',
        font=f
        ).place(x=150, y=320)

    Label(
        win, 
        text='Net Salary:', 
        font=cf
        ).place(x=250, y=320)

    Label(
        win, 
        text=f'{net_sal}', 
        font=f
        ).place(x=390, y=320)

    Button(
        win, 
        text='Close', 
        padx=10, 
        pady=5, 
        font=f, 
        bg='#FF614F', 
        command=lambda:win.destroy()
        ).place(x=220, y=390)

Output:

READ:  Create a game using Python Pygame (Tic tac toe game)

In this output, the window shares employee information and the deductions made in the salary. Net salary is the in-hand amount that an employee is going to receive. The close button at the bottom will close this window.

Payslip GUI application using Python Tkinter
Payslip GUI application using Python Tkinter

In this tutorial, we have learned how to create a payslip application using a console and how to Generate Payslip using Python Tkinter. Write us if you have any queries or suggestions.

  • Simple Payslip application using python console
  • Simple payslip application using python Tkinter

Related Python Tkinter tutorials: