A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-loan-calculator-using-tkinter/ below:

Python | Loan calculator using Tkinter

Python | Loan calculator using Tkinter

Last Updated : 11 Jul, 2025

Prerequisite: Tkinter Introduction
 

Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.
 

To create a Tkinter :

  1. Importing the module – Tkinter
  2. Create the main window (container)
  3. Add any number of widgets to the main window
  4. Apply the event Trigger on the widgets.

The GUI will look like below:


Let's see how to create a loan calculator using Python GUI library Tkinter. The calculator will be capable of calculating the total amount and monthly payment based on the loan amount, period and interest rate. 
Step #1: Create the main window. 
 

Python3
    def __init__(self):
        # Create a window
        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)

        # 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) 
        # Create an event loop
        window.mainloop() 

  
Step #2: Adding functionality.
 

Python3
    def computePayment(self):
        # compute the total payment.
        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'))

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

        return monthlyPayment;

  
Step #3: Complete program 
 

Python3
 # Import tkinter
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: 


Code Explanation: 
 


Why we used sticky argument? 
By default, widgets are created in the center, using sticky arguments we can change it. It takes 4 values: N, S, E, W. i.e. North, East, South, West.
 


Loan calculator using Tkinter


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4