In this tutorial, you will learn how to create a graphical salary calculator using Python's Tkinter library. The application calculates hourly wages, daily salaries, and monthly and annual salaries based on user input. By the end of this tutorial, you'll have a functional and visually appealing salary calculator.

Prerequisites

Before starting, ensure you have Python installed on your system. You'll also need the Pillow library for handling images in Tkinter. You can install Pillow by running:

pip install pillow

Step 1: setting up the environment

We'll begin by importing the necessary libraries and defining some constants for the application:

from tkinter import *
from tkinter import Tk, StringVar, ttk
from PIL import Image, ImageTk

# Define color constants
co0 = "#2e2d2b"  # Black
co1 = "#feffff"  # White
co2 = "#4fa882"  # Green
co3 = "#38576b"  # Ash blue
co4 = "#403d3d"  # Ash
co5 = "#e06636"  # Orange
co6 = "#038cfc"  # Blue
co7 = "#3fbfb9"  # Green
co8 = "#263238"  # Black
co9 = "#e9edf5"  # White

# Initialize the main window
window = Tk()
window.title("Salary Calculator")
window.geometry('500x350')
window.configure(background=co9)
window.resizable(width=FALSE, height=FALSE)

style = ttk.Style(window)
style.theme_use("clam")

Step 2: creating the layout

We'll create a structured layout with two frames: one for the header and another for user inputs and results.

# Top frame for the header
top_frame = Frame(window, width=1043, height=60, pady=5, bg=co1, relief="flat")
top_frame.grid(row=0, column=0)

# Bottom frame for input fields and results
bottom_frame = Frame(window, width=1043, height=400, bg=co1, pady=0, relief="flat")
bottom_frame.grid(row=1, column=0, pady=0, padx=0, sticky=NSEW)

# Add an app logo and title
app_lg = Image.open('salary.png')
app_lg = app_lg.resize((25, 25))
app_lg = ImageTk.PhotoImage(app_lg)
app_logo = Label(top_frame, image=app_lg, text=" Salary Calculator", width=500, compound=LEFT, relief=RAISED, anchor=NW, font=('Verdana 15'), bg=co1, fg=co4)
app_logo.place(x=0, y=0)

Step 3: adding input fields

Next, we'll create fields for user input, including name, hours worked per day, days worked per week, and annual salary.

# User input fields
l_name = Label(bottom_frame, text="Write your full name *", height=1, anchor=NW, font=('Ivy 10'), bg=co1, fg=co4)
l_name.place(x=10, y=10)
e_name = Entry(bottom_frame, width=25, justify='left', relief="solid")
e_name.place(x=200, y=11)

l_hours = Label(bottom_frame, text="How many hours do you work per day?", height=1, anchor=NW, font=('Ivy 10'), bg=co1, fg=co4)
l_hours.place(x=10, y=40)
e_hours = Entry(bottom_frame, width=15, justify='center', relief="solid")
e_hours.place(x=260, y=41)

l_days_work = Label(bottom_frame, text="How many days do you work per week?", height=1, anchor=NW, font=('Ivy 10'), bg=co1, fg=co4)
l_days_work.place(x=10, y=70)
e_days_work = Entry(bottom_frame, width=15, justify='center', relief="solid")
e_days_work.place(x=260, y=71)

l_salary = Label(bottom_frame, text="What is your Annual Salary?", height=1, anchor=NW, font=('Ivy 10'), bg=co1, fg=co4)
l_salary.place(x=10, y=100)
e_salary = Entry(bottom_frame, width=15, justify='center', relief="solid")
e_salary.place(x=260, y=101)

Step 4: implementing the salary calculation logic

Here's the core function that calculates salaries based on the provided inputs:

def salary():
    year = 365
    weeks = 52

    name = e_name.get()
    hours_per_day = int(e_hours.get())
    work_days = int(e_days_work.get())
    annual_salary = float(e_salary.get())

    # Calculate working hours
    weekly_working_hours = hours_per_day * work_days
    annually_working_hours = weeks * weekly_working_hours

    # Salary breakdown
    hourly_wage = annual_salary / annually_working_hours
    daily_salary = hourly_wage * 8
    weekly_salary = annual_salary / 52
    monthly_salary = annual_salary / 12

    # Display results
    l_name['text'] = name
    l_days['text'] = f'Working hours per day: {hours_per_day}'
    l_h_days['text'] = f'Working days per week: {work_days}'
    l_anual['text'] = f'$ {annual_salary:,.2f}'
    l_hour['text'] = f'Hourly wage: $ {hourly_wage:,.2f}'
    l_day['text'] = f'Daily Salary: $ {daily_salary:,.2f}'
    l_week['text'] = f'Weekly Salary: $ {weekly_salary:,.2f}'
    l_month['text'] = f'Monthly Salary: $ {monthly_salary:,.2f}'

Step 5: adding a button to trigger calculations

A button is required to execute the salary calculation:

calculate_button = Button(bottom_frame, command=salary, text="Calculate", width=10, overrelief=RIDGE, font=('ivy 10 bold'), bg=co1, fg=co0)
calculate_button.place(x=370, y=92)

Step 6: displaying results

Finally, we display the calculated results in the bottom frame:

# Result display labels
l_days = Label(bottom_frame, width=25, text='', relief=RAISED, anchor=NW, padx=5, pady=5, font=('Ivy 10'), bg=co9, fg=co0)
l_days.place(x=10, y=180)
l_h_days = Label(bottom_frame, width=25, text='', relief=RAISED, anchor=NW, padx=5, pady=5, font=('Ivy 10'), bg=co9, fg=co0)
l_h_days.place(x=10, y=210)
l_anual = Label(bottom_frame, width=25, text='', relief=RAISED, anchor=NW, padx=5, pady=5, font=('Ivy 10 bold'), bg=co9, fg=co0)
l_anual.place(x=10, y=240)

Step 7: running the application

Add the following line at the end of the script to start the application:

window.mainloop()

Congratulations! You have successfully built a salary calculator with Python and Tkinter. Customize the colors, fonts, and layout further to match your preferences.

Copyright Notice: Unless otherwise indicated, all articles are original to this site, and reproduction must cite the source

Article link:http://pybeginners.com/article/building-a-salary-calculator-with-python-and-tkinter/