Javatpoint Logo
Javatpoint Logo

Loan Calculator using PyQt5 in Python

This article will demonstrate how to use PyQt5 to develop a loan calculator.

A complete set of Python bindings for Qt v5 is available as PyQt5. Cross-platform GUI toolkit PyQt5 is a collection of Python bindings for Qt version 5. The capabilities and simplicity offered by this toolkit make it quite simple to create an interactive desktop application. On all platforms that are supported, including iOS and Android, it enables Python to be used as an alternative to C++ for application development. It is implemented as more than 35 extension modules.

This post is mainly about building a loan calculator using the Python library called PyQt5. A loan is a legal agreement between a borrower and a lender whereby the borrower gets a sum of money (the principle) that they must later repay. And, to calculate this value, we use Loan Calculator. This Loan Calculator will be built using the PyQt5 library.

So, we will start with the basic instructions to be followed.

Steps for implementing a GUI :

  1. Make a heading label with the name of the calculator.
  2. Make a label and line edit pair for the interest rate, with the label indicating what the user must write and the line edit allowing text entry.
  3. Create a pair in a similar manner for the amount and the year.
  4. Construct a calculator push button.
  5. Design a label that displays the estimated monthly payment.
  6. Make a label to display the total amount computed.

Implementation steps on the back end :

  1. Change the line edit to only take numbers as input;
  2. Add a push button action.
  3. Within the push button action, retrieve the line edits' text.
  4. Determine if the line edit text is empty or zero, and then return to prevent continued execution of the procedure.
  5. Change the value of the text to an integer.
  6. Determine the monthly sum and add it to the label.
  7. Add up the monthly amounts to get the total, then label the result.

Walking through the code :

First of all, to begin with, we imported all the required modules into our program. The files we imported include QtWidgets, QtCore, QtGui and sys.

Then, we created a new class which contains the constructor to initialise our function. Next, we set the title, width and height of the window. Also, we defined the window's geometry. Then, we displayed all the widgets present.

Then we defined another function in which we will be creating the heading and various labels and setting their properties. Next, we set the heading for the loan calculator and set its position and geometry along with the font properties and colour effects.

Then, we created various labels and set their properties. The labels were annual interest, years and amount. They were first created, then we defined their properties and set their positions. Then we also defined their font styles and sizes for displaying. Along with these labels, we also created simultaneous labels for the text to be entered or displayed, i.e. textareas.

Then we created a push button and payment labels. For the button, we first created it, then set its geometry and then added functionality to it in order to perform a function on being clicked. Then we created a monthly payment label and defined its properties and font. Similarly, we created a yearly payment label and defined its properties and fonts too.

Then, we created a function to calculate monthly and yearly payments. The entire result was based also on the annual interest rate variable's value and the no. of months value and the values which were not in numerical form were converted to integers from text. And after the final calculation the value was derived by a certain mentioned formula and was stored in total payment variable.

Finally, we created the instance of the window for displaying the application and then executed the application by calling the exit() method from the system.

Complete Code :


The output of the above-mentioned code is attached below as a screenshot.

Loan Calculator using PyQt5 in Python

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA