0% found this document useful (0 votes)
44 views4 pages

Gui Lab8

This document contains code for creating a graphical calculator application using Tkinter in Python. It defines functions for creating frames, buttons, and an entry field widget to display calculations. It also includes functions for evaluating mathematical expressions typed into the entry field and clearing the field when buttons are pressed. The main app class initializes the window, packs the widgets, and binds button press events to trigger the calculation functions.

Uploaded by

Sarah Sonali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views4 pages

Gui Lab8

This document contains code for creating a graphical calculator application using Tkinter in Python. It defines functions for creating frames, buttons, and an entry field widget to display calculations. It also includes functions for evaluating mathematical expressions typed into the entry field and clearing the field when buttons are pressed. The main app class initializes the window, packs the widgets, and binds button press events to trigger the calculation functions.

Uploaded by

Sarah Sonali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

8/18/2020 guicalculator1 - Jupyter Notebook

In [2]:

1 from tkinter import *


2
3 def iCalc(source, side):
4 storeObj = Frame(source, borderwidth=4, bd=4, bg="powder blue")
5 storeObj.pack(side=side, expand =YES, fill =BOTH)
6 return storeObj
7
8 def button(source, side, text, command=None):
9 storeObj = Button(source, text=text, command=command)
10 storeObj.pack(side=side, expand = YES, fill=BOTH)
11 return storeObj
12
13 class app(Frame):
14 def __init__(self):
15 Frame.__init__(self)
16 self.option_add('*Font', 'arial 20 bold')
17 self.pack(expand = YES, fill =BOTH)
18 self.master.title('Calculator')
19
20 display = StringVar()
21 Entry(self, relief=RIDGE, textvariable=display,
22 justify='right'
23 , bd=30, bg="powder blue").pack(side=TOP,
24 expand=YES, fill=BOTH)
25
26 for clearButton in (["C"]):
27 erase = iCalc(self, TOP)
28 for ichar in clearButton:
29 button(erase, LEFT, ichar, lambda
30 storeObj=display, q=ichar: storeObj.set(''))
31
32 for numButton in ("789/", "456*", "123-", "0.+"):
33 FunctionNum = iCalc(self, TOP)
34 for iEquals in numButton:
35 button(FunctionNum, LEFT, iEquals, lambda storeObj=display, q=iEquals:
36
37 EqualButton = iCalc(self, TOP)
38 for iEquals in "=":
39 if iEquals == '=':
40 btniEquals = button(EqualButton, LEFT, iEquals)
41 btniEquals.bind('<ButtonRelease-1>', lambda e,s=self,
42 storeObj=display: s.calc(storeObj), '+')
43
44
45 else:
46 btniEquals = button(EqualButton, LEFT, iEquals,
47 lambda storeObj=display, s=' %s ' % iEquals: storeO
48 (storeObj.get() + s))
49
50 def calc(self, display):
51 try:
52 display.set(eval(display.get()))
53 except:
54 display.set("ERROR")
55
56
57 if __name__=='__main__':
58 app().mainloop()

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

60 b2 = tk.Button(root, text='Monthly Payment',


61 command=(lambda e=ents: monthly_payment(e)))
62 b2.pack(side=tk.LEFT, padx=5, pady=5)
63 b3 = tk.Button(root, text='Quit', command=root.quit)
64 b3.pack(side=tk.LEFT, padx=5, pady=5)
65 root.mainloop()

In [ ]:

localhost:8888/notebooks/Documents/python/guicalculator1.ipynb 5/5

You might also like