Tip Calculator using Python

Tip Calculator using Python

Introduction

Hello friends, welcome to copyassignment.com. In this tutorial, we are going to develop a simple project of Tip Calculator using Python a GUI-based project using the Python Tkinter. The Tip Calculator calculates tip amount for various percentages of the cost of the services, it also provides a total amount that includes the tip. This code is easily understandable to all python beginners who want to learn python.

In this Python program for Tip Calculator, we are providing comments and explanations for an easy understanding of the code.

So let’s understand the program in detail

Tip Calculator in Python: Project Details

Project Name:Tip Calculator using Python
AbstractIt’s a GUI-based project used with the Tkinter module to calculate tip amounts for various percentages of the cost of the services,
Language/s Used:Python
IDEPycharm(Recommended)
Python version (Recommended):3.8 or 3.9
Type:Desktop Application

Features of Tip calculator using Python

The tip calculator performs the following task:

1. Accept the bill.

2 . Accepts the tip amount and calculates the tip for the percentage cost of services.

3. Calculates the total bill

4. Reset the fields.

Use of Pycharm IDE for Project

  1. First Install Pycharm Community Edition 2021.3.1 (community edition is to be installed)
  2. Create New Project by clicking on File and selecting New Project, writing the project name, and clicking on “Create”.
PyCharm setup 1 for Tip Calculator using Python
Pycharm setup1 for Tip Calculator using Python

3. Right-click on the project name you have created and  Create a New Python File as “tipcalculator.py”.

Pycharm setup2 for Tip Calculator using Python
Pycharm setup2 for Tip Calculator using Python

4. Write the code in the file and execute the Tip Calculator using Python by Clicking the “Run” tab.

Pycharm setup3 for Tip Calculator using Python
Pycharm setup3 for Tip Calculator using Python

Note: You can install the modules by going to “File”->” Settings”-> ”Project: TipCalculator”->” Python Interpreter”->click on the”+” sign and write the name of the module want to install and click on “Install package”.

Code flow: Tip Calculator in python with source code

Importing the libraries

#import libraries
from tkinter import Tk,Radiobutton
from tkinter import Button
from tkinter import Label,StringVar,IntVar,Entry

Explanation:

These modules are used for the following purposes:

1.    Tkinter – To create the GUI.   

2.  Radio buttons are used for selection

3.  Buttons are used for applying the code changes.

4.  Label is used for giving a name to the button or text box.

5.  StringVar and IntVar are the datatypes of string and integer type

Defining the class and initializing the variables

class TipCalculator():
    def __init__(self):
        w=Tk() # create the w object of Tk() to access the window functions
        w.title("Tip Calculator")#Set the title for the window
        w.geometry("650x300")#set the size of the window

        # Intialize the variables
        self.m_cost = StringVar()
        self.t_p = IntVar()
        self.tip = StringVar()
        self.total_cost = StringVar()

 

Explanation:

In this code block, we have defined a class TipCalculator, where we have defined__ init__as  a constructor. The size and title of the window are defined. We have defined the 4 variables for m_cost for meal cost, tip for tip and total_cost for the total cost of string type, and t_p for tip percentage of Integer type.

Defining the GUI window for Tip Calculator using Python

t_p = Label(w, text="Tip Percentage", font=('Calibri 20 bold'), bg="Blue", fg="white")
t_p.grid(column=0, row=0, padx=40, pady=10)
five_tip = Radiobutton(w, text="5%", variable=self.t_p, value=5)
five_tip.grid(column=1, row=0)
ten_tip = Radiobutton(w, text="10%", variable=self.t_p, value=10)
ten_tip.grid(column=2, row=0)
fifteen_tip = Radiobutton(w, text="15%", variable=self.t_p, value=15)
fifteen_tip.grid(column=3, row=0)
twenty_tip = Radiobutton(w, text="20%", variable=self.t_p, value=20)
twenty_tip.grid(column=4, row=0)
twentyfive_tip = Radiobutton(w, text="25%", variable=self.t_p, value=25)
twentyfive_tip.grid(column=5, row=0)
thirty_tip = Radiobutton(w, text="30%", variable=self.t_p, value=30)
thirty_tip.grid(column=6, row=0)

bill_details = Label(w, text="Bill Details", font=('Calibri 20 bold'), bg="Blue", fg="white")
bill_details.grid(column=0, row=1,padx=10,pady=10)
bill_amount_entry = Entry(w, textvariable=self.m_cost, width=20)
bill_amount_entry.grid(column=1, row=1, padx=15)

tip_amount_lbl = Label(w, text="Tip Amount entry", font=('Calibri 20 bold'), bg="blue", fg="white")
tip_amount_lbl.grid(column=0, row=2,padx=10,pady=10)
tip_amount_entry = Entry(w, textvariable=self.tip, width=20)
tip_amount_entry.grid(column=1, row=2, padx=10)

bill_total_lbl = Label(w, text="Whats the total bill", font=('Calibri 20 bold'), bg="Blue", fg="white")
bill_total_lbl.grid(column=0, row=3,padx=10,pady=10)
bill_total_entry = Entry(w, textvariable=self.total_cost, width=20)
bill_total_entry.grid(column=1, row=3,padx=10)

#Button for resetting the values
clear_btn = Button(w, text="reset", bg="Orange", font=('Calibri 20 bold'), fg="white", command=self.reset)
clear_btn.grid(column=0, row=4)

#Button to calculate the Tip
calculate_btn = Button(w, text="Calculate", bg="Blue", font=('Calibri 20 bold'), fg="white",
                       command=self.calculate)
calculate_btn.grid(column=1, row=4)

#Function to close the window to view the output
w.mainloop()

Explanation:

In this code block, we have defined the GUI window for Tip Calculator,

Tip Percentage: It is used for displaying the various percentage radio button for the user selection for calculating the tip, We have defined the percentages as 5,10,15,20,25,30.

Bill Details- To enter the bill amount.

Tip amount entry -To enter the tip amount that is provided.

Total Bill- The total Bill calculates the total bill depending on the percentage cost of services and the tip amount.

Output

Output1 of Tip Calculator using Python
Output1 of Tip Calculator using Python

Function for resetting the value

#Function for resetting the values
def reset(self):
    self.total_cost.set("")
    self.m_cost.set("")
    self.tip.set("")

Explanation:

The reset function clears the text fields in front of the labels to add the new value in the fields.

Function for Calculating the final bill

#Function for calculating the tip  
def calculate(self):
        p_tip = float(self.m_cost.get())
        per = self.t_p.get() / 100
        tip_amount_entry_var = p_tip * per
        self.tip.set(tip_amount_entry_var)
        final_bill = p_tip + tip_amount_entry_var
        self.total_cost.set(final_bill)

Explanation:

Here we are calculating the total bill after applying the tip percentage to the meal cost. The m_cost variable takes the meal cost in the float datatype form in the variable p_tip. The per variable calculates the percentage by taking the value of t_p. The meal cost and percentage are calculated and stored in tip_amount_entr_var.

The final bill contains the addition of the meal cost and the value in tip_amount_entry_var which sets the value to the total_cost.

Output

Output2 of Tip Calculator using Python
Output2 of Tip Calculator using Python

Complete code for Tip Calculator using Python

#import libraries
from tkinter import Tk,Radiobutton
from tkinter import Button
from tkinter import Label,StringVar,IntVar,Entry

class TipCalculator():
    def __init__(self):
        w=Tk()
        w.title("Tip Calculator")
        w.geometry("650x300")

        # Intialize the variables
        self.m_cost = StringVar()
        self.t_p = IntVar()
        self.tip = StringVar()
        self.total_cost = StringVar()

        t_p = Label(w, text="Tip Percentage", font=('Calibri 20 bold'), bg="Blue", fg="white")
        t_p.grid(column=0, row=0, padx=40, pady=10)
        five_tip = Radiobutton(w, text="5%", variable=self.t_p, value=5)
        five_tip.grid(column=1, row=0)
        ten_tip = Radiobutton(w, text="10%", variable=self.t_p, value=10)
        ten_tip.grid(column=2, row=0)
        fifteen_tip = Radiobutton(w, text="15%", variable=self.t_p, value=15)
        fifteen_tip.grid(column=3, row=0)
        twenty_tip = Radiobutton(w, text="20%", variable=self.t_p, value=20)
        twenty_tip.grid(column=4, row=0)
        twentyfive_tip = Radiobutton(w, text="25%", variable=self.t_p, value=25)
        twentyfive_tip.grid(column=5, row=0)
        thirty_tip = Radiobutton(w, text="30%", variable=self.t_p, value=30)
        thirty_tip.grid(column=6, row=0)

        bill_details = Label(w, text="Bill Details", font=('Calibri 20 bold'), bg="Blue", fg="white")
        bill_details.grid(column=0, row=1,padx=10,pady=10)
        bill_amount_entry = Entry(w, textvariable=self.m_cost, width=20)
        bill_amount_entry.grid(column=1, row=1, padx=15)

        tip_amount_lbl = Label(w, text="Tip Amount entry", font=('Calibri 20 bold'), bg="blue", fg="white")
        tip_amount_lbl.grid(column=0, row=2,padx=10,pady=10)
        tip_amount_entry = Entry(w, textvariable=self.tip, width=20)
        tip_amount_entry.grid(column=1, row=2, padx=10)

        bill_total_lbl = Label(w, text="Total Bill", font=('Calibri 20 bold'), bg="Blue", fg="white")
        bill_total_lbl.grid(column=0, row=3,padx=10,pady=10)
        bill_total_entry = Entry(w, textvariable=self.total_cost, width=20)
        bill_total_entry.grid(column=1, row=3,padx=10)

        clear_btn = Button(w, text="reset", bg="Orange", font=('Calibri 20 bold'), fg="white", command=self.reset)
        clear_btn.grid(column=0, row=4)

        calculate_btn = Button(w, text="Calculate", bg="Blue", font=('Calibri 20 bold'), fg="white",
                               command=self.calculate)
        calculate_btn.grid(column=1, row=4)

        w.mainloop()

    def calculate(self):
        p_tip = float(self.m_cost.get())
        per = self.t_p.get() / 100
        tip_amount_entry_var = p_tip * per
        self.tip.set(tip_amount_entry_var)
        final_bill = p_tip + tip_amount_entry_var
        self.total_cost.set(final_bill)

    def reset(self):
        self.total_cost.set("")
        self.m_cost.set("")
        self.tip.set("")


TipCalculator()

Summary

Here we have developed a simple Tip Calculator using Python Tkinter library with source code where we can easily calculate the tip amount for various percentages of cost services.

We hope you find this article helpful. For more articles on python keep visiting our site.

Thank you for watching this article.


Also Read:

 

Share:

Author: Ayush Purawr