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:
