Loan Calculator Using Python

Create a Loan Calculator Using Python

Create a Loan Calculator Using Python

Hello coder, welcome to the codewithrandom blog. In this blog, we will learn to Create a Loan Calculator Using Python. A loan calculator is a program that helps you calculate the monthly payment, total interest, and total cost of a loan. Building a loan calculator using Python and tkinter can be a great way to practice your programming skills and create a useful program that can help you make financial decisions.

Loan Calculator Using Python

we’ll go over the steps you need to take to build your own Loan Calculator Using Python.

Step 1: Install tkinter library .

First, you’ll need to make sure that you have tkinter installed on your computer. Tkinter is a Python library that allows you to create graphical user interfaces (GUIs). If you don’t already have tkinter installed, you can install it using pip.

$ pip install tkinter

Step 2: Import tkinter

Once you have tkinter installed, you can import it into your Python code and create a window.

from tkinter import *

step 2: Create a Function to calculate lone & Create a window or set title

class LoanCalculator:

    def __init__(self):
        window = Tk()  
        window.title("Loan Calculator")  # Set title
        # create the input boxes.
        Label(window, text="Annual Interest Rate").grid(row=1,
                                                        column=1, sticky=W)
        Label(window, text="Number of Years").grid(row=2,
                                                   column=1, sticky=W)
        Label(window, text="Loan Amount").grid(row=3,
                                               column=1, sticky=W)
        Label(window, text="Monthly Payment").grid(row=4,
                                                   column=1, sticky=W)
        Label(window, text="Total Payment").grid(row=5,
                                                 column=1, sticky=W)

step 3 : for taking inputs

self.annualInterestRateVar = StringVar()
Entry(window, textvariable=self.annualInterestRateVar,
      justify=RIGHT).grid(row=1, column=2)
self.numberOfYearsVar = StringVar()

Entry(window, textvariable=self.numberOfYearsVar,
      justify=RIGHT).grid(row=2, column=2)
self.loanAmountVar = StringVar()

Entry(window, textvariable=self.loanAmountVar,
      justify=RIGHT).grid(row=3, column=2)
self.monthlyPaymentVar = StringVar()
lblMonthlyPayment = Label(window, textvariable=
self.monthlyPaymentVar).grid(row=4,
                             column=2, sticky=E)

self.totalPaymentVar = StringVar()
lblTotalPayment = Label(window, textvariable=
self.totalPaymentVar).grid(row=5,
                           column=2, sticky=E)

step 4 :  create the button & an event loop

 
btComputePayment = Button(window, text="Compute Payment",
                          command=self.computePayment).grid(
    row=6, column=2, sticky=E)
window.mainloop()  # Create an event loop

step 5: Compute the total & monthly payment

    def computePayment(self):
        monthlyPayment = self.getMonthlyPayment(
            float(self.loanAmountVar.get()),
            float(self.annualInterestRateVar.get()) / 1200,
            int(self.numberOfYearsVar.get()))

        self.monthlyPaymentVar.set(format(monthlyPayment, '10.2f'))
        totalPayment = float(self.monthlyPaymentVar.get()) * 12 \
                       * int(self.numberOfYearsVar.get())

        self.totalPaymentVar.set(format(totalPayment, '10.2f'))

    def getMonthlyPayment(self, loanAmount, monthlyInterestRate, numberOfYears):
       
        monthlyPayment = loanAmount * monthlyInterestRate / (1
                                                             - 1 / (1 + monthlyInterestRate) ** (numberOfYears * 12))
        return monthlyPayment;
        root = Tk()  # create the widget


# call the class to run the program.
LoanCalculator()

Step 6: Run the program

Now that you have all the code, you can run the program and test it out.

Lone calculator using python

Complete Source Code For the Build a Lone Calculator Using Python(copy the code and run )👇👇👇

from tkinter import *


class LoanCalculator:

    def __init__(self):
        window = Tk()  # Create a window
        window.title("Loan Calculator")  # Set title
        # create the input boxes.
        Label(window, text="Annual Interest Rate").grid(row=1,
                                                        column=1, sticky=W)
        Label(window, text="Number of Years").grid(row=2,
                                                   column=1, sticky=W)
        Label(window, text="Loan Amount").grid(row=3,
                                               column=1, sticky=W)
        Label(window, text="Monthly Payment").grid(row=4,
                                                   column=1, sticky=W)
        Label(window, text="Total Payment").grid(row=5,
                                                 column=1, sticky=W)

        # for taking inputs
        self.annualInterestRateVar = StringVar()
        Entry(window, textvariable=self.annualInterestRateVar,
              justify=RIGHT).grid(row=1, column=2)
        self.numberOfYearsVar = StringVar()

        Entry(window, textvariable=self.numberOfYearsVar,
              justify=RIGHT).grid(row=2, column=2)
        self.loanAmountVar = StringVar()

        Entry(window, textvariable=self.loanAmountVar,
              justify=RIGHT).grid(row=3, column=2)
        self.monthlyPaymentVar = StringVar()
        lblMonthlyPayment = Label(window, textvariable=
        self.monthlyPaymentVar).grid(row=4,
                                     column=2, sticky=E)

        self.totalPaymentVar = StringVar()
        lblTotalPayment = Label(window, textvariable=
        self.totalPaymentVar).grid(row=5,
                                   column=2, sticky=E)

        # create the button
        btComputePayment = Button(window, text="Compute Payment",
                                  command=self.computePayment).grid(
            row=6, column=2, sticky=E)
        window.mainloop()  # Create an event loop

    # compute the total payment.
    def computePayment(self):
        monthlyPayment = self.getMonthlyPayment(
            float(self.loanAmountVar.get()),
            float(self.annualInterestRateVar.get()) / 1200,
            int(self.numberOfYearsVar.get()))

        self.monthlyPaymentVar.set(format(monthlyPayment, '10.2f'))
        totalPayment = float(self.monthlyPaymentVar.get()) * 12 \
                       * int(self.numberOfYearsVar.get())

        self.totalPaymentVar.set(format(totalPayment, '10.2f'))

    def getMonthlyPayment(self, loanAmount, monthlyInterestRate, numberOfYears):
        # compute the monthly payment.
        monthlyPayment = loanAmount * monthlyInterestRate / (1
                                                             - 1 / (1 + monthlyInterestRate) ** (numberOfYears * 12))
        return monthlyPayment;
        root = Tk()  # create the widget


# call the class to run the program.
LoanCalculator()

Output for Lone Calculator Using Python 👇👇👇

Lone calculator using python

Conclusion

Building a loan calculator using Python and tkinter can be a fun and useful project to take on. In this blog post, we’ve covered the steps you need to take to create a loan calculator that calculates monthly payments, total interest, and total cost.

By following these steps, you can create a loan calculator using Python that helps you make informed financial decisions. You can customize the program to include additional features or create a more sophisticated user interface.

Whether you’re a beginner or an experienced programmer, building a loan calculator is a great way to improve your coding skills and develop a useful tool. Hope you enjoyed building with us! Visit our homepage and you get lot’s of projects💝

#loan calculator using Python



Leave a Reply