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