Here’s a Python program that creates a simple GUI-based Compound Interest Calculator using the tkinter library.
The calculator allows users to input the principal amount, annual interest rate, number of times interest is compounded per year, and the number of years.
The program then calculates the compound interest and displays the result.
Code
import tkinter as tk from tkinter import messagebox # Function to calculate compound interest def calculate_compound_interest(): try: principal = float(principal_entry.get()) rate = float(rate_entry.get()) times = int(times_entry.get()) years = int(years_entry.get()) # Compound interest formula: A = P * (1 + r/n)^(n*t) amount = principal * (1 + rate / (100 * times)) ** (times * years) interest = amount - principal # Display the result result_label.config(text=f"Compound Interest: ${interest:.2f}\nTotal Amount: ${amount:.2f}") except ValueError: messagebox.showerror("Input Error", "Please enter valid numerical values.") # Initialize the main window root = tk.Tk() root.title("Compound Interest Calculator") root.geometry("400x400") # Title Label title_label = tk.Label(root, text="Compound Interest Calculator", font=("Helvetica", 16)) title_label.pack(pady=10) # Input fields tk.Label(root, text="Principal Amount (P):").pack() principal_entry = tk.Entry(root) principal_entry.pack(pady=5) tk.Label(root, text="Annual Interest Rate (r) %:").pack() rate_entry = tk.Entry(root) rate_entry.pack(pady=5) tk.Label(root, text="Times Compounded per Year (n):").pack() times_entry = tk.Entry(root) times_entry.pack(pady=5) tk.Label(root, text="Number of Years (t):").pack() years_entry = tk.Entry(root) years_entry.pack(pady=5) # Calculate Button calculate_button = tk.Button(root, text="Calculate", command=calculate_compound_interest) calculate_button.pack(pady=20) # Result Label result_label = tk.Label(root, text="Compound Interest: \nTotal Amount: ", font=("Helvetica", 12)) result_label.pack(pady=20) # Run the application root.mainloop()
Explanation of the Code
- Import Required Libraries:
- tkinter: Used to create the GUI components.
- messagebox from tkinter: Displays error messages if the user inputs invalid data.
- Define calculate_compound_interest Function:
- This function is triggered when the “Calculate” button is clicked.
- Retrieve User Input:
- principal = float(principal_entry.get()): Retrieves and converts the principal amount from the entry box.
- rate = float(rate_entry.get()): Retrieves the annual interest rate.
- times = int(times_entry.get()): Retrieves the number of times interest is compounded per year.
- years = int(years_entry.get()): Retrieves the number of years.
- Calculate Compound Interest:
- Formula: A=P×(1+rn×100)n×tA = P \times \left(1 + \frac{r}{n \times 100}\right)^{n \times t}
- amount = principal * (1 + rate / (100 * times)) ** (times * years): Calculates the total amount.
- interest = amount – principal: Calculates the compound interest by subtracting the principal from the total amount.
- Display the Result:
- result_label.config(…) updates the result label to display the calculated compound interest and the total amount.
- Error Handling:
- If the user inputs invalid data (e.g., non-numeric values), a ValueError will occur, and messagebox.showerror(…) will display an error message.
- Initialize the Main Application Window:
- root = tk.Tk(): Creates the main application window.
- root.title(“Compound Interest Calculator”): Sets the window title.
- root.geometry(“400×400”): Sets the window size to 400×400 pixels.
- Title Label:
- title_label = tk.Label(…): Displays the title of the application at the top.
- .pack(pady=10): Adds vertical padding around the label.
- Input Fields for Principal, Rate, Times Compounded, and Years:
- Each input field consists of a Label and an Entry widget.
- The labels describe what each field is for (e.g., “Principal Amount (P):”).
- The entry widgets allow the user to input values for each variable.
- .pack(pady=5): Adds spacing around each input field for readability.
- Calculate Button:
- calculate_button = tk.Button(…): Creates a button labeled “Calculate” that triggers the calculate_compound_interest function when clicked.
- Result Label:
- result_label = tk.Label(…): Displays the calculated compound interest and total amount.
- Initially, it shows placeholder text, but it updates with the calculated values after clicking “Calculate”.
- Run the Main Event Loop:
- root.mainloop(): Starts the tkinter main event loop, keeping the application window open and responsive to user input.
Sample Calculation
If the user inputs:
- Principal Amount: 1000
- Annual Interest Rate: 5%
- Times Compounded per Year: 4
- Number of Years: 10
The result might look like this:
Compound Interest: $647.01 Total Amount: $1647.01
Explanation of Compound Interest Formula
The formula for compound interest is:
Where:
- AA is the final amount.
- PP is the principal (initial amount).
- rr is the annual interest rate (in %).
- nn is the number of times interest is compounded per year.
- tt is the number of years.
Notes
- Input Validation: The program checks if the user inputs are valid numbers using a try-except block. If not, it shows an error message.
- Real-Time Updates: After each calculation, the result label updates to show the compound interest and total amount.
Summary
This Python program is a GUI-based Compound Interest Calculator built with tkinter.
It allows users to enter the principal, interest rate, compounding frequency, and years, and it calculates the compound interest and total amount.
The program includes error handling to ensure that only valid numerical inputs are processed.