It is a great personal project to build to practice your Python skills. Furthermore, this tutorial will teach you how to create the application in two ways, first as a command-line tool and second as a GUI tool.

Preview

We are going to build the application in two ways. First, we will build a simple Python shell script that prompts the user for input and writes the output. Second, we will give the program a Graphical User Interface using Tkinter.

Program Specification

The program receives three inputs:

The bill amount The tipping percentage The number of people sharing the bill

Using these inputs, the program will compute the following output:

Each person’s contribution to the bill Each person’s contribution to the tip Each person’s total contribution

Algorithm

To achieve this, the Tip and Split calculator will follow a very simple algorithm outlined below:

Prerequisites

To follow this tutorial, you should know and understand Python programming language. For this tutorial, knowledge of basic concepts, including functions and classes, is required. In addition, you should have Python installed in your system. If it is not, head over to the Python website and download it. Alternatively, Geekflare has an online Python compiler where you can run your Python code in the browser with no environment setup at all.

Building the Calculator with a Command Line Interface

Create a Project Folder

To begin, navigate to an empty folder in your system. In my case, I am using Ubuntu 22.04, so to create a folder and navigate to it using the terminal; I need to enter the following command:

Create the Python File

Next, create the script file where we will write the Python script. In my case, I will use the touch command to do so:

Open the Script File with Your Favourite Code Editor

To begin writing the code to the script, open the file with your favourite code editor. I am going to be using nano which is a terminal-based text editor.

Receive the Input

With this done, we can add the following lines of code to the top of the file: Basically, this receives the input and casts the data type of each input from a string to the most appropriate type.

Calculate the Tip Amount

Next, we calculate the tip amount by multiplying the tip percentage by the bill amount.

Divide the Bill and Tip to Get Each Person’s Contribution to the Two

Calculate the Total Contribution

Next, add the individual contributions to determine the total contribution per person.

Display the Results

Lastly, output the results to the user.

Testing the Tip and Split Calculator

Finally, your script file should look like this: At this point, feel free to test-run your application using the following command:

Building the Tip and Split Calculator with GUI

In the next portion of this tutorial, we will implement the same application but with a Graphical User Interface. To build the GUI, we will use a package called Tkinter.

Setting Up

Tkinter is a package built into Python’s Standard Library. This means it was installed by default when you installed Python. However, on Linux machines with Python installed by default, TKinter is not preinstalled to save space. Therefore, you need to install it manually using the following command:

Create a Project File

To begin, create a file where the Python script will be stored. After you create the file, open it with your preferred text editor.

Import Tkinter

Next, import the Tkinter package by adding the following line to the top of the file.

Create the User Interface

Then, we can begin creating the user interface. The above code created a window containing all the User Interface widgets. In addition, it created a label that will serve as the window’s title. Next, it created labels and entry fields for the three inputs: bill_amount, tip_percentage and number_of_people. Lastly, it created a button that the user will click to run the calculation.

Create a Function to Calculate the Outputs

After this, we can create a function to handle the click of the Enter button. This function will take the values of the entry fields and use them to calculate the outputs using the algorithm mentioned before. Lastly, it will create a label to display the output and update the window. The code in the above function has been explained through comments explaining each major step.

Attaching the Event Handler to the Button

Next, we bind the event handler to the button click event. The button click event in Tkinter is represented by the string ‘‘. To bind the event to the event handler, we use the bind method of the button. Add this line of code beneath the function definition: Lastly, to keep the window running, we call the mainloop method of the window object. And we are done!

Testing the Tip and Split Calculator

You can run the application using the following command: This should open up the window as follows: You can run the calculator with sample input:

Final Words

In this tutorial, we created a tip and split calculator in two ways. The first uses a terminal-based CLI tool. The second is a GUI tool using Python’s Tkinter. This tutorial shows how to build a simple Python project. If you need to brush up or polish your Python skills, here is a Datacamp course. Next, you can check out how to create a random password generator in Python.

How to Create a Tip and Split Calculator in Python - 31How to Create a Tip and Split Calculator in Python - 72How to Create a Tip and Split Calculator in Python - 58How to Create a Tip and Split Calculator in Python - 46How to Create a Tip and Split Calculator in Python - 48How to Create a Tip and Split Calculator in Python - 41How to Create a Tip and Split Calculator in Python - 58How to Create a Tip and Split Calculator in Python - 96How to Create a Tip and Split Calculator in Python - 85How to Create a Tip and Split Calculator in Python - 10How to Create a Tip and Split Calculator in Python - 47How to Create a Tip and Split Calculator in Python - 22How to Create a Tip and Split Calculator in Python - 22How to Create a Tip and Split Calculator in Python - 47How to Create a Tip and Split Calculator in Python - 5How to Create a Tip and Split Calculator in Python - 67How to Create a Tip and Split Calculator in Python - 90How to Create a Tip and Split Calculator in Python - 29How to Create a Tip and Split Calculator in Python - 90How to Create a Tip and Split Calculator in Python - 4How to Create a Tip and Split Calculator in Python - 81How to Create a Tip and Split Calculator in Python - 28How to Create a Tip and Split Calculator in Python - 3How to Create a Tip and Split Calculator in Python - 75How to Create a Tip and Split Calculator in Python - 7How to Create a Tip and Split Calculator in Python - 2How to Create a Tip and Split Calculator in Python - 83How to Create a Tip and Split Calculator in Python - 14