Gui Lab8
Gui Lab8
In [2]:
localhost:8888/notebooks/Documents/python/guicalculator1.ipynb 2/5
8/18/2020 guicalculator1 - Jupyter Notebook
localhost:8888/notebooks/Documents/python/guicalculator1.ipynb 3/5
8/18/2020 guicalculator1 - Jupyter Notebook
In [ ]:
1 import tkinter as tk
2
3 fields = ('Annual Rate', 'Number of Payments', 'Loan Principle', 'Monthly Payment', 'Re
4
5 def monthly_payment(entries):
6 # period rate:
7 r = (float(entries['Annual Rate'].get()) / 100) / 12
8 print("r", r)
9 # principal loan:
10 loan = float(entries['Loan Principle'].get())
11 n = float(entries['Number of Payments'].get())
12 remaining_loan = float(entries['Remaining Loan'].get())
13 q = (1 + r)** n
14 monthly = r * ( (q * loan - remaining_loan) / ( q - 1 ))
15 monthly = ("%8.2f" % monthly).strip()
16 entries['Monthly Payment'].delete(0, tk.END)
17 entries['Monthly Payment'].insert(0, monthly )
18 print("Monthly Payment: %f" % float(monthly))
19
20 def final_balance(entries):
21 # period rate:
22 r = (float(entries['Annual Rate'].get()) / 100) / 12
23 print("r", r)
24 # principal loan:
25 loan = float(entries['Loan Principle'].get())
26 n = float(entries['Number of Payments'].get())
27 monthly = float(entries['Monthly Payment'].get())
28 q = (1 + r) ** n
29 remaining = q * loan - ( (q - 1) / r) * monthly
30 remaining = ("%8.2f" % remaining).strip()
31 entries['Remaining Loan'].delete(0, tk.END)
32 entries['Remaining Loan'].insert(0, remaining )
33 print("Remaining Loan: %f" % float(remaining))
34
35 def makeform(root, fields):
36 entries = {}
37 for field in fields:
38 print(field)
39 row = tk.Frame(root)
40 lab = tk.Label(row, width=22, text=field+": ", anchor='w')
41 ent = tk.Entry(row)
42 ent.insert(0, "0")
43 row.pack(side=tk.TOP,
44 fill=tk.X,
45 padx=5,
46 pady=5)
47 lab.pack(side=tk.LEFT)
48 ent.pack(side=tk.RIGHT,
49 expand=tk.YES,
50 fill=tk.X)
51 entries[field] = ent
52 return entries
53
54 if __name__ == '__main__':
55 root = tk.Tk()
56 ents = makeform(root, fields)
57 b1 = tk.Button(root, text='Final Balance',
58 command=(lambda e=ents: final_balance(e)))
59 b1.pack(side=tk.LEFT, padx=5, pady=5)
localhost:8888/notebooks/Documents/python/guicalculator1.ipynb 4/5
8/18/2020 guicalculator1 - Jupyter Notebook
In [ ]:
localhost:8888/notebooks/Documents/python/guicalculator1.ipynb 5/5