Calculate Compound Interest using Python in Few Steps

Rahul Patodi
DataFlair
Published in
4 min readJust now

This project uses the Tkinter framework in Python to provide a basic implementation of a compound interest calculator.

Users can enter the principal amount, interest rate, number of compound interest periods annually, and time period in years. The compound interest is then calculated by the calculator, and the result is shown in Indian Rupees (₹). It has an easy-to-use UI with instant feedback.

Prerequisites for Python Compound Interest Calculator

To implement this Python Compound Interest Calculator Project successfully, you should have:

  • Python Programming: Basic understanding of Python, including syntax, data structures, functions, and handling exceptions.
  • Tkinter: knowledge of Tkinter for managing events, handling widgets (buttons, labels, etc.), generating GUI windows, and organizing layouts with grid or pack managers.
  • Mathematics: Understanding of the compound interest formula.
Python Compound Interest Calculator

Project Implementation of Python Compound Interest Calculator

Importing Necessary Libraries and Modules:

import tkinter as tk
from tkinter import messagebox

Explanation: This part imports the tkinter library, which is the standard GUI toolkit in Python. The messagebox submodule is used to display messages to the user.

CompoundInterestCalculator Class Definition:

Initialization

 def calculate_compound_interest():
try:
principal = float(entry_principal.get())
rate = float(entry_rate.get())
times_compounded = int(entry_times_compounded.get())
time = float(entry_time.get())

# Calculate compound interest
amount = principal * (1 + (rate / (times_compounded * 100)))**(times_compounded * time)
compound_interest = amount - principal


label_result.config(text=f"Compound Interest: ₹{compound_interest:,.2f}")
except ValueError:
messagebox.showerror("Input Error", "Please enter valid numerical values.")

Explanation: The calculate_compound_interest function retrieves user input from the entry fields and calculates the compound interest using the formula:

Where:

  • P is the principal amount
  • r is the annual interest rate
  • n is the number of times interest is compounded per year
  • t is the time in years

The compound interest is then calculated as A−PA — PA−P, and the result is displayed on a label. If the user input is invalid, an error message is shown.

Creating Widgets:

 # Set up the main window
root = tk.Tk()
root.title("Compound Interest Calculator")


# Create and place the labels and entry widgets
tk.Label(root, text="Principal Amount (₹):").grid(row=0, column=0, padx=10, pady=5)
entry_principal = tk.Entry(root)
entry_principal.grid(row=0, column=1, padx=10, pady=5)


tk.Label(root, text="Rate of Interest (%):").grid(row=1, column=0, padx=10, pady=5)
entry_rate = tk.Entry(root)
entry_rate.grid(row=1, column=1, padx=10, pady=5)


tk.Label(root, text="Times Compounded per Year:").grid(row=2, column=0, padx=10, pady=5)
entry_times_compounded = tk.Entry(root)
entry_times_compounded.grid(row=2, column=1, padx=10, pady=5)


tk.Label(root, text="Time (Years):").grid(row=3, column=0, padx=10, pady=5)
entry_time = tk.Entry(root)
entry_time.grid(row=3, column=1, padx=10, pady=5)


# Create and place the calculate button
btn_calculate = tk.Button(root, text="Calculate", command=calculate_compound_interest)
btn_calculate.grid(row=4, column=0, columnspan=2, pady=10)


# Create and place the result label
label_result = tk.Label(root, text="Compound Interest: ₹0.00")
label_result.grid(row=5, column=0, columnspan=2, pady=10)


# Run the main loop
root.mainloop()

Explanation:

  • Main Window Setup: This part sets up the main Tkinter window and assigns it a title “Compound Interest Calculator”.
  • Labels and Entry Widgets: Labels and entry widgets are created for each input field: principal amount, rate of interest, times compounded per year, and time in years. These widgets are placed in a grid layout using the grid method.
  • Calculate Button: A button is created, which, when clicked, calls the calculate_compound_interest function to compute the compound interest.
  • Result Label: A label is created to display the calculated compound interest. Initially, it shows “Compound Interest: ₹0.00”.
  • Main Loop: The mainloop method starts the Tkinter event loop, waiting for user interaction.

Full Code:

import tkinter as tk
from tkinter import messagebox


def calculate_compound_interest():
try:
principal = float(entry_principal.get())
rate = float(entry_rate.get())
times_compounded = int(entry_times_compounded.get())
time = float(entry_time.get())

# Calculate compound interest
amount = principal * (1 + (rate / (times_compounded * 100)))**(times_compounded * time)
compound_interest = amount - principal


label_result.config(text=f"Compound Interest: ₹{compound_interest:,.2f}")
except ValueError:
messagebox.showerror("Input Error", "Please enter valid numerical values.")


# Set up the main window
root = tk.Tk()
root.title("Compound Interest Calculator")


# Create and place the labels and entry widgets
tk.Label(root, text="Principal Amount (₹):").grid(row=0, column=0, padx=10, pady=5)
entry_principal = tk.Entry(root)
entry_principal.grid(row=0, column=1, padx=10, pady=5)


tk.Label(root, text="Rate of Interest (%):").grid(row=1, column=0, padx=10, pady=5)
entry_rate = tk.Entry(root)
entry_rate.grid(row=1, column=1, padx=10, pady=5)


tk.Label(root, text="Times Compounded per Year:").grid(row=2, column=0, padx=10, pady=5)
entry_times_compounded = tk.Entry(root)
entry_times_compounded.grid(row=2, column=1, padx=10, pady=5)


tk.Label(root, text="Time (Years):").grid(row=3, column=0, padx=10, pady=5)
entry_time = tk.Entry(root)
entry_time.grid(row=3, column=1, padx=10, pady=5)


# Create and place the calculate button
btn_calculate = tk.Button(root, text="Calculate", command=calculate_compound_interest)
btn_calculate.grid(row=4, column=0, columnspan=2, pady=10)


# Create and place the result label
label_result = tk.Label(root, text="Compound Interest: ₹0.00")
label_result.grid(row=5, column=0, columnspan=2, pady=10)


# Run the main loop
root.mainloop()

Python Compound Interest Calculator Output

Python Compound Interest Calculator Project Output
Python Compound Interest Calculator Output
Compound Interest Calculator Project Output

Conclusion:

This project uses the Tkinter toolkit in Python to create a simple graphical user interface (GUI) compound interest calculator.

To calculate compound interest, the user must enter the principal amount, interest rate, number of compounding interest periods annually, and time period.

With the help of this Python Project, users may efficiently compute compound interest in Indian Rupees and see the result displayed in a format that uses a thousand separators and the sign ₹.

This Python Project reinforces knowledge of the compound interest formula and its practical applications while showcasing fundamental Tkinter features.

--

--