Introduction
Hello friends, welcome to violet-cat-415996.hostingersite.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 |
Abstract | It’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 |
IDE | Pycharm(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
- First Install Pycharm Community Edition 2021.3.1 (community edition is to be installed)
- Create New Project by clicking on File and selecting New Project, writing the project name, and clicking on “Create”.
3. Right-click on the project name you have created and Create a New Python File as “tipcalculator.py”.
4. Write the code in the file and execute the Tip Calculator using Python by Clicking the “Run” tab.
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
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
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:
- Create your own ChatGPT with Python
- Bakery Management System in Python | Class 12 Project
- SQLite | CRUD Operations in Python
- Event Management System Project in Python
- Ticket Booking and Management in Python
- Hostel Management System Project in Python
- Sales Management System Project in Python
- Bank Management System Project in C++
- Python Download File from URL | 4 Methods
- Python Programming Examples | Fundamental Programs in Python
- Spell Checker in Python
- Portfolio Management System in Python
- Stickman Game in Python
- Contact Book project in Python
- Loan Management System Project in Python
- Cab Booking System in Python
- Brick Breaker Game in Python
- Tank game in Python
- GUI Piano in Python
- Ludo Game in Python
- Rock Paper Scissors Game in Python
- Snake and Ladder Game in Python
- Puzzle Game in Python
- Medical Store Management System Project in Python
- Creating Dino Game in Python
- Tic Tac Toe Game in Python
- Test Typing Speed using Python App
- MoviePy: Python Video Editing Library
- Scientific Calculator in Python
- GUI To-Do List App in Python Tkinter