forked from jigar-sable/Awesome_Python_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloan_calculator.py
79 lines (53 loc) · 3.49 KB
/
loan_calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Import TKinter Module
from tkinter import * # (*) Imports whole module
# Create a user defined class named: LoanCalculator which holds it's own
# data members and member functions.
class LoanCalculator:
def __init__(self): #Special method in Python Class-Constructor of a Python Class
window = Tk() #Creates a window to house the calculator bits
window.title("Loan Calculator") # sets the title
window.configure(background = "light green") # sets bg color for window
# Create input boxes: Label function creates a display box to take input
# The grid method gives it a table like structure
#Widgets are centered by default.
Label(window, font='Helvetica 12 bold',bg ="light green",text="Annual Interest Rate").grid(row=1,column=1, sticky=W)
Label(window, font='Helvetica 12 bold',bg ="light green",text="Number of Years").grid(row=2,column=1, sticky=W)
Label(window, font='Helvetica 12 bold',bg ="light green",text="Loan Amount").grid(row=3,column=1, sticky=W)
Label(window, font='Helvetica 12 bold',bg ="light green", text="Monthly Payment").grid(row=4,column=1, sticky=W)
Label(window, font='Helvetica 12 bold',bg ="light green",text="Total Payment").grid(row=5,column=1, sticky=W)
# Create objects: first 3 objects to take inputs using entry() function
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,font='Helvetica 12 bold',bg ="light green", textvariable=self.monthlyPaymentVar).grid(row=4,column=2, sticky=E)
self.totalPaymentVar = StringVar()
lblTotalPayment= Label(window,font='Helvetica 12 bold',bg ="light green", textvariable=self.totalPaymentVar).grid(row=5,column=2, sticky=E)
# Create button to calculate payment. When button is clicked it will call the compute payment function
btComputePayment = Button(window, text="Compute Payment",bg="red",fg="white",font='Helvetica 14 bold', command=self.computePayment).grid(row=6, column=2, sticky=E)
btClear = Button(window, text="Clear",bg="blue",fg="white",font='Helvetica 14 bold', command=self.delete_all).grid(row=6, column=8, padx=20,pady=20 ,sticky=E)
window.mainloop() # The mainloop () function is used to run the application program.
# Create function to compute 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):
monthlyPayment = loanAmount * monthlyInterestRate / (1-1/(1 + monthlyInterestRate)** (numberofYears * 12 ))
return monthlyPayment
def delete_all(self) :
self.monthlyPaymentVar.set("")
self.loanAmountVar.set("")
self.annualInterestRateVar.set("")
self.numberofYearsVar.set("")
self.totalPaymentVar.set("")
# Call the class to run the program
LoanCalculator()