×

Loan calculator using Tkinter in Python

Tkinter:

Tkinter, part of all common Python distributions, is the de facto method for creating Graphical User Interfaces (GUIs) in Python. The only framework included in the Python standard library is this one. On top of Tk, this Python framework serves as a thin object-oriented layer that provides access to the Tk toolkit. The Tk toolkit is a cross-platform set of "graphical control elements," also called widgets, used to build application interfaces. This framework gives Python users a quick and easy way to build GUI elements with the Tk toolkit's widgets. In a Python application, Tk widgets can be used to create buttons, menus, data fields, etc.

Once created, these graphic elements can be connected to or work with other widgets, features, functionality, processes, or data.

Tkinter bridges the gap between Python's execution model and Tcl's, built around cooperative multitasking. Like Python, Tcl is an interpreted dynamic programming language. Although it is an overall programming language that can be used independently, it is most frequently used in C programs as a scripting motor or as an interaction with the Tk toolbox. The Tcl library has a C interface that enables users to add custom Tcl or C commands, run Tcl commands and scripts, and manage one or more Tcl interpreter instances. Each interpreter has a queue for events that can be used to send and process events.

Tk is a C implementation of the Tcl package that adds new custom commands for generating and modifying GUI widgets. Tk is loaded into each Tk object's embedded Tcl interpreter instance. The high degree of customization offered by Tk's widgets comes at the expense of a dated design. To create and handle GUI events, Tk uses the Tcl event queue.ssss

A more recent family of Tk widgets called Themed Tk (Ttk) offers a much better appearance on various platforms than many traditional Tk widgets. Starting with version 8.5 of Tk, Ttk is distributed alongside Tk. Tkinter. ttk, a separate module, offers Python bindings.

The Tkinter module first creates a Tcl/Tk command string when your Python application uses a Tkinter class to create a widget. This internal _tkinter boolean subsystem calls the Tcl translator to process the Tcl control string after receiving it. The Tcl interpreter will then consult the Tk and Ttk packages, which will call Xlib, Cocoa, or GDIafter that.

Advantages of Tkinter:

Programming flexibility - Qt's GUI programming is based on the concept of signals and slots for establishing contact between objects. This promotes flexibility in handling GUI incidents, which makes the code base run more smoothly.

Qt is more than just a framework; it uses a variety of native platform APIs for networking, database development, and other uses. Through a unique API, it gives them direct access.

Different UI elements: Qt offers several widgets, like buttons or menus, created with a fundamental interface for all compatible platforms.

Learning resources: PyQt is one of Python’s most popular UI systems, making it easy to access various documentation.

Disadvantages of Tkinter:

PyQt5 classes lack Python-specific documentation. It takes much time to learn all the details of PyQt, so the learning curve is steep. You must purchase a commercial license if the application is not open-source. Tkinter doesn't have any advanced widgets. There isn't a tool like Qt Designer for Tkinter in it. Its user interface is unreliable. Tkinter can be challenging to debug at times. It's not entirely in Python.

CODE:

Creating the Main Window:

def __init__(self):
    window = Tk()
    window.title("Loan Calculator")
    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)
    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)
    btComputePayment = Button(window, text = "Compute Payment",
                           command = self.computePayment).grid(
                               row = 6, column = 2, sticky = E)
    window.mainloop()

Adding functionality:

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;

Entire Program (Loan Calculator):

from tkinter import *
class LoanCalculator:
    def __init__(self):
        window = Tk() # Create a window
        window.title("Loan Calculator") # Set title
        # create the input boxes.
                                          column = 1, sticky = W)        Label(window, text = "Annual Interest Rate").grid(row = 1,


        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)
        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)
        btComputePayment = Button(window, text = "Compute Payment",
                                  command = self.computePayment).grid(
                                  row = 6, column = 2, sticky = E)
        window.mainloop() 
 
    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() 
LoanCalculator()

OUTPUT:

Loan calculator using Tkinter in Python

Related Topics

Scraping data in python

Data scraping is a technique in which one program extracts a set of data from the output of another program. Web scraping is the most common application of this technique....

6 minutes read.

Python Rest API

In this tutorial, we will understand the meaning of API and REST API. We will understand the working of REST API. We will then realize the boundaries of architecture defined...

4 minutes read.

Python String rstrip() method

Python String rstrip() method The string.rstrip() method in Python returns a copy of the string with trailing characters removed. Syntax string.rstrip([chars]) Parameter chars:  This argument represents a string specifying the set of characters to be...

1 minute read.

Python String upper() method

Python String upper() method The string.upper() method in Python returns a copy of the string converted to uppercase. Syntax string.upper() Parameter NA Return This method returns a copy of the string converted to uppercase. Example 1 # Python...

1 minute read.

List in Python

What is List in Python In Python, lists are used to store the multiple values in one variable. We can say that list is the collection of similar as well as...

3 minutes read.

How to Print Pattern in Python

How to Print Pattern in Python. Pattern programs are really important for beginners and they are always asked in all the technical interviews. Multiple loops approach is used to print the different...

6 minutes read.

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal We know that the most widely used number system is a decimal system, but the computer only understands binary values. The...

2 minutes read.

How to Download all Modules in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Standard Scalar in Python

Python is a vast and open-source language that contains many libraries, modules, and functions. These functions in python are reusable codes where we don’t need to type the whole code...

4 minutes read.

Continue and Pass Statements in Python

Difference between continue and pass statements in Python The tasks can be repeated and automated efficiently using loops in Python. Sometimes we have to exit from the entire loop completely, we...

2 minutes read.

GUI to extract lyrics from a song Using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

4 minutes read.

How to Fix an EOF Error in Python?

Introduction An EOF (End-of-File) error occurs when a program tries to read beyond the end of a file or a stream, causing it to return an error message. It can happen...

8 minutes read.

ComboBox in Python

Python includes several graphical user interface (GUI) libraries, including PyQT, Tkinter, Kivy, WxPython, and PySide. Tkinter is the most commonly used GUI module in Python because it is simple and...

2 minutes read.

_name_ in Python

Introduction: The code at level 0 indentation is to be performed when the command to run a Python program is supplied to the interpreter because Python does not have a main() function...

4 minutes read.

Python Queue

Python Queue There are various day to day activities where we find ourselves engaged with queues. Whether it is waiting in toll tax lane or standing on the billing counter for...

7 minutes read.

Python Graph

Python Graph: In Computer Science and Mathematics, a Graph is a pictorial representation of a group of objects or elements where some elements are connected using the links. A graph...

5 minutes read.

Python for Loop Increment

Introduction In general, loops are employed for sequential traversal. It belongs to the definite iteration category. Definite iterations imply that the number of iterations is explicitly set in advance.  In this article,...

4 minutes read.

Python Syntax

Python is a strong object-oriented programming language that is simple to learn. Python was created to be a very readable programming language. The syntax of the Python programming language is...

8 minutes read.

What is Python compiler GDB?

The source code of one programming language is converted into machine code, bytecode, or another programming language by a compiler, a specialised software. A compiler is a tool that converts high-level...

3 minutes read.

How to Install Numpy in PyCharm

What is PyCharm? JetBrains created the hybrid platform known as PyCharm as a Python IDE. The Python IDE PyCharm is used by some major companies, including Twitter, Facebook, Amazon, and many...

4 minutes read.