-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.py
147 lines (115 loc) · 6.8 KB
/
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from tkinter import *
root = Tk()
#button_9 = Button(label_key,text='9',height=3,width=5,font=('Helvetica','12'))
#button_9.grid(row=0,column=0)
class Calculator:
def click_button(self,numbers):
global operator
global var
self.operator = self.operator + str(numbers)
self.var.set(self.operator)
def clear(self):
self.entry.delete(0,END)
self.operator =""
def delete(self):
self.operator = str(self.entry.delete(len(self.entry.get())-1))
def evaluate(self):
self.answer =eval(self.entry.get())
self.var.set(self.answer)
self.operator = str(self.answer)
def __init__(self,master):
self.operator = ""
self.var = StringVar()
frame_s = Frame(master, height=400, width=45 )
frame_s.pack(side=TOP, fill=BOTH, expand=True)
self.entry = Entry(frame_s,textvariable=self.var,bg='grey',width=45,bd=20,insertwidth=4,justify='right',font=('arial',10,'bold'))
self.entry.pack()
self.t = Text(self.entry,height=40)
label_key = Label(root, height=15, width=30,bd=10,bg='gray50')
label_key.pack(side=LEFT, fill=BOTH, expand=True)
label_fkey = Label(root, height=15, width=15, bg='gray25')
label_fkey.pack(fill=BOTH, expand=True)
label_7 = Label(label_key, bg='black')
label_7.grid(row=0, column=0)
button_7 = Button(label_7, text='7', font=('Helvetica', '16'),command= lambda : self.click_button(7),bg='black',fg='WHITE')
button_7.pack()
label_8 = Label(label_key, bg='black')
label_8.grid(row=0, column=1, padx=20)
button_8 = Button(label_8, text='8', font=('Helvetica', '16'),command= lambda: self.click_button(8),bg='black',fg='WHITE')
button_8.pack()
label_9 = Label(label_key, bg='black')
label_9.grid(row=0, column=2, padx=10)
button_9 = Button(label_9, text='9', font=('Helvetica', '16'),command= lambda: self.click_button(9),bg='black',fg='WHITE')
button_9.pack()
label_4 = Label(label_key, bg='black')
label_4.grid(row=1, column=0, padx=10, pady=10)
button_4 = Button(label_4, text='4', font=('Helvetica', '16'),command= lambda: self.click_button(4),bg='black',fg='WHITE')
button_4.pack()
label_5 = Label(label_key, bg='black')
label_5.grid(row=1, column=1, padx=10, pady=10)
button_5 = Button(label_5, text='5', font=('Helvetica', '16'),command= lambda: self.click_button(5),bg='black',fg='WHITE')
button_5.pack()
label_6 = Label(label_key, bg='black')
label_6.grid(row=1, column=2, padx=10, pady=10)
button_6 = Button(label_6, text='6', font=('Helvetica', '16'),command= lambda: self.click_button(6),bg='black',fg='WHITE')
button_6.pack()
label_1 = Label(label_key, bg='black')
label_1.grid(row=2, column=0, padx=10)
button_1 = Button(label_1, text='1', font=('Helvetica', '16'),command= lambda: self.click_button(1),bg='black',fg='WHITE')
button_1.pack()
label_2 = Label(label_key, bg='black')
label_2.grid(row=2, column=1, padx=10)
button_2 = Button(label_2, text='2', font=('Helvetica', '16'),command= lambda: self.click_button(2),bg='black',fg='WHITE')
button_2.pack()
label_3 = Label(label_key, bg='black')
label_3.grid(row=2, column=2, padx=10)
button_3 = Button(label_3, text='3', font=('Helvetica', '16'),command= lambda: self.click_button(3),bg='black',fg='WHITE')
button_3.pack()
label_0 = Label(label_key, bg='black')
label_0.grid(row=3, column=0, padx=10, pady=10)
button_0 = Button(label_0, text='0', font=('Helvetica', '16'),command= lambda: self.click_button(0),bg='black',fg='WHITE')
button_0.pack()
label_deci = Label(label_key, bg='black')
label_deci.grid(row=3, column=1, padx=10, pady=10)
button_deci = Button(label_deci, text='.', font=('Helvetica', '16'),command= lambda: self.click_button('.'),bg='black',fg='WHITE')
button_deci.pack()
label_equal = Label(label_key, bg='black')
label_equal.grid(row=3, column=2, padx=10, pady=10)
button_equal = Button(label_equal, text='=', font=('Helvetica', '16'),command= self.evaluate,bg='black',fg='WHITE')
button_equal.pack()
label_C = Label(label_fkey, bg='black')
label_C.grid(row=0, column=0,columnspan=2)
button_C = Button(label_C, text='C', font=('Helvetica', '16'), height=1, width=10,command= self.clear,bg='black',fg='WHITE')
button_C.pack(side=LEFT)
label_sub = Label(label_fkey, bg='black')
label_sub.grid(row=1, column=0, sticky=W, pady=10)
button_sub = Button(label_sub, text='-', font=('Helvetica', '16'), height=1, width=3,command= lambda: self.click_button('-'),bg='black',fg='WHITE')
button_sub.pack(side=LEFT)
label_mul = Label(label_fkey, bg='black')
label_mul.grid(row=1, column=1, sticky=E)
button_mul = Button(label_mul, text='x', font=('Helvetica', '16'), height=1, width=3,command= lambda: self.click_button('*'),bg='black',fg='WHITE')
button_mul.pack()
label_div = Label(label_fkey, bg='black')
label_div.grid(row=2, column=0, sticky=W)
button_div = Button(label_div, text='/', font=('Helvetica', '16'), height=1, width=3,command= lambda: self.click_button('/'),bg='black',fg='WHITE')
button_div.pack()
label_add = Label(label_fkey, bg='black')
label_add.grid(row=2, column=1, sticky=E)
button_add = Button(label_add, text='+', font=('Helvetica', '16'), height=1, width=3,command= lambda: self.click_button('+'),bg='black',fg='WHITE')
button_add.pack()
label_lbrace = Label(label_fkey, bg='black')
label_lbrace.grid(row=3,column=0,sticky=W,pady=10)
button_lbrace = Button(label_lbrace,text='(', font=('Helvetica', '16'), height=1, width=3,command= lambda: self.click_button('('),bg='black',fg='WHITE')
button_lbrace.pack()
label_rbrace = Label(label_fkey, bg='black')
label_rbrace.grid(row=3, column=1, sticky=E, pady=10)
button_rbrace = Button(label_rbrace, text=')', font=('Helvetica', '16'), height=1, width=3,
command=lambda: self.click_button(')'),bg='black',fg='WHITE')
button_rbrace.pack()
label_del = Label(label_fkey, bg ='black')
label_del.grid(row=4,column=0,columnspan=2)
button_del = Button(label_del, text='Del', font=('Helvetica', '16'),bd=3, height=1, width=10,command= self.delete,bg='black',fg='WHITE') # Changed command to self.delete
button_del.pack()
c = Calculator(root)
root.title("Godfrey\'s Calculator")
root.mainloop()