/
investment.py
67 lines (55 loc) · 2.32 KB
/
investment.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
'''
Program: investment.py
Autho:Nick
Date: 4/28/2020
page 276
'''
from breezypythongui import EasyFrame
class Investment(EasyFrame):
'''An investment calculator that demosnstrates the use of a multiline text area'''
def __init__(self):
'''Sets up the window and widgets'''
EasyFrame.__init__(self, title = "Investment Calculator")
#Labels for the window
self.addLabel(text = "Initial Amount", row = 0, column = 0)
self.addLabel(text = "Number of Years", row = 1, column = 0)
self.addLabel(text = "Interest Rate in %", row = 2, column = 0)
#Entry fields
self.amount = self.addFloatField(value = 0.0, row = 0, column = 1)
self.period = self.addIntegerField(value = 0, row = 1, column = 1)
self.rate = self.addIntegerField(value = 0, row = 2, column = 1)
#Button
self.compute = self.addButton(text = "Compute", row = 3, column = 0, columnspan = 2, command = self.compute)
#Text area widget
self.outputArea = self.addTextArea(text = "", row = 4, column = 0, columnspan = 2, width = 50, height = 15)
#Event handling methods
def compute(self):
'''Computes the investment schedule based on the inputs and outputs the schedule in the text area'''
#Obtain and validate the inputs
startBalance = self.amount.getNumber()
rate = self.rate.getNumber()/100
years = self.period.getNumber()
if startBalance == 0 or rate == 0 or years == 0:
return #this gets you out of the function
#Set the header for the table
result = "%4s%18s%10s%16s\n" % ("Year", "Starting Balance", "Interest", "Ending Balance")
#Compute and append the results for each year
totalInterest = 0.0
for year in range(1, years + 1):
interest = startBalance * rate
endBalance = startBalance + interest
result += "%4d%18.2f%10.2f%16.2f\n" % (year, startBalance, interest, endBalance)
startBalance = endBalance
totalInterest += interest
#Append the totals for the period
result += "Ending balance: $%0.2f\n" % endBalance
result += "Total interest earned: $%0.2f\n" % totalInterest
#Output the result whil preserving read only status
self.outputArea["state"] = "normal"
self.outputArea.setText(result)
self.outputArea["state"] = "disabled"
def main():
'''Instantiates and pops up the window'''
Investment().mainloop()
#Global call to main() function
main()