Home » General Python Tutorials » How to Make a Simple Interest Calculator using Python

How to Make a Simple Interest Calculator using Python

In today’s article, we are going to create a simple calculator that calculates interest using the tkinter library, where the user can input their data and, by just clicking the calculate button, receive the calculated interest. This builds upon our previous exploration of crafting a codebase for a simple calculator using the tkinter library in Python. Without further ado, let’s dive into how we can extend that basic calculator into a more functional application that can handle interest calculations.

Table of Contents

Necessary Libraries

For this code to work correctly, it’s necessary to install the tkinter library through the terminal or command prompt.

$ pip install tk 

Imports

from tkinter import *

First, we import everything from the tkinter library, allowing us to use the graphical user interface (GUI).

Class Definition

class SimpleInterestCalculator:

Then we create a class basically the blueprint for our interest Calculator.

Initializer Method

def __init__(self, master):

What we did here is start with the setup phase which is a set of instructions that prepare the setup of the window.

Setting up the Window

 self.master = master
       master.title("Simple Interest Calculator - The Pycodes")
       master.geometry("500x300")

Here, we gave our interest Calculator a name and defined its geometry.

Creating Labels

      self.principal_label = Label(master, text="Principal:", font="arial 15")
       self.rate_label = Label(master, text="Rate Of Interest:", font="arial 14")
       self.time_label = Label(master, text="Time:", font="arial 15")
       self.interest_label = Label(master, text="Interest:", font="arial 15")

After that, we give Labels in other words, we create name tags in a specific font.

Placing the Labels on the Window

       self.principal_label.place(x=50, y=20)
       self.rate_label.place(x=50, y=90)
       self.time_label.place(x=50, y=160)
       self.interest_label.place(x=50, y=230)

The code presented here basically tells the computer where to put the name tags that we created.

Creating Variables to Hold User Input

       self.principal_value = StringVar()
       self.rate_value = StringVar()
       self.time_value = StringVar()

In this part, we create a number of containers where the user’s input is held.

Creating Entry Widgets

self.principal_entry = Entry(master, textvariable=self.principal_value, font="arial 20", width=8)
       self.rate_entry = Entry(master, textvariable=self.rate_value, font="arial 20", width=8)
       self.time_entry = Entry(master, textvariable=self.time_value, font="arial 20", width=8)

Next, we create Entry boxes where the user can type his input.

Placing Entry Widgets and Adding the Result Label

       self.principal_entry.place(x=200, y=20)
       self.rate_entry.place(x=200, y=90)
       self.time_entry.place(x=200, y=160)

       self.result_label = Label(master, text="", font="arial 30 bold")
       self.result_label.place(x=200, y=220)

This code informs the computers where to put the entry boxes, as well as creates the result label where the result of the calculation will appear and chooses a place for it.

Creating Buttons

       self.calculate_button = Button(master, text="Calculate", font="arial 15", command=self.calculate)
       self.calculate_button.place(x=350, y=20)

       self.clear_button = Button(master, text="Clear", font="arial 15", command=self.clear_entries)
       self.clear_button.place(x=350, y=90)

       self.exit_button = Button(master, text="Exit", command=master.destroy, font="arial 15", width=8)
       self.exit_button.place(x=350, y=160)

Following that, we create three buttons once clicked each one of them triggers something, the first button is “Calculate” which calls the calculate function, the second one is called “Clear” which calls the clear_entries function, the last one is called “Exit” which as the name suggests Exit the window.

Calculate Function

   def calculate(self):
       try:
           principal = float(self.principal_value.get())
           rate = float(self.rate_value.get())
           time = float(self.time_value.get())
           amount = (principal * rate * time) / 100
           self.result_label.config(text=f"{amount}")
       except ValueError:
           self.result_label.config(text="Invalid input. Please enter valid numbers.")

Then, we define a function that gets triggered once the user clicks the “Calculate” button, what this function does is ensure the numbers inputted by the user are converted to floats for the amount calculation, if there is a conversion issue, an error message appears giving feedback to the user. If not, the calculated interest amount appears on the result label.

Clear_entries Function

   def clear_entries(self):
       self.principal_value.set("")
       self.rate_value.set("")
       self.time_value.set("")
       self.result_label.config(text="")

Here, we created a function that gets triggered once the user clicks the “Clear” button, what this function does is simply set the principal, rate, and time entry widgets to their initial states and also clear the result label.

Setting up the Main Window

if __name__ == "__main__":
   root = Tk()
   app = SimpleInterestCalculator(root)
   root.mainloop()

Lastly, this part creates the main window and keeps it in a loop (open) until the user chooses to exit the said window.

Example

Full Code

from tkinter import *


class SimpleInterestCalculator:
   def __init__(self, master):
       self.master = master
       master.title("Simple Interest Calculator - The Pycodes")
       master.geometry("500x300")


       self.principal_label = Label(master, text="Principal:", font="arial 15")
       self.rate_label = Label(master, text="Rate Of Interest:", font="arial 14")
       self.time_label = Label(master, text="Time:", font="arial 15")


       self.principal_label.place(x=50, y=20)
       self.rate_label.place(x=50, y=90)
       self.time_label.place(x=50, y=160)


       self.interest_label = Label(master, text="Interest:", font="arial 15")
       self.interest_label.place(x=50, y=230)


       self.principal_value = StringVar()
       self.rate_value = StringVar()
       self.time_value = StringVar()


       self.principal_entry = Entry(master, textvariable=self.principal_value, font="arial 20", width=8)
       self.rate_entry = Entry(master, textvariable=self.rate_value, font="arial 20", width=8)
       self.time_entry = Entry(master, textvariable=self.time_value, font="arial 20", width=8)


       self.principal_entry.place(x=200, y=20)
       self.rate_entry.place(x=200, y=90)
       self.time_entry.place(x=200, y=160)


       self.result_label = Label(master, text="", font="arial 30 bold")
       self.result_label.place(x=200, y=220)


       self.calculate_button = Button(master, text="Calculate", font="arial 15", command=self.calculate)
       self.calculate_button.place(x=350, y=20)


       self.clear_button = Button(master, text="Clear", font="arial 15", command=self.clear_entries)
       self.clear_button.place(x=350, y=90)


       self.exit_button = Button(master, text="Exit", command=master.destroy, font="arial 15", width=8)
       self.exit_button.place(x=350, y=160)


   def calculate(self):
       try:
           principal = float(self.principal_value.get())
           rate = float(self.rate_value.get())
           time = float(self.time_value.get())
           amount = (principal * rate * time) / 100
           self.result_label.config(text=f"{amount}")
       except ValueError:
           self.result_label.config(text="Invalid input. Please enter valid numbers.")


   def clear_entries(self):
       self.principal_value.set("")
       self.rate_value.set("")
       self.time_value.set("")
       self.result_label.config(text="")


if __name__ == "__main__":
   root = Tk()
   app = SimpleInterestCalculator(root)
   root.mainloop()

Happy Coding!

Learn also: How to Make a Simple Calculator using Python tkinter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top