1
+ #import Gui Package and libraries
1
2
import tkinter .font
2
3
from tkinter import *
3
4
import math
4
5
6
+
5
7
def clearall ():
8
+
9
+ '''Clear function toclear the screen
10
+ and reset all variables'''
11
+
6
12
global expression
7
13
global equation
8
14
global value
@@ -13,9 +19,14 @@ def clearall():
13
19
equation .set (expression )
14
20
15
21
def sgn (a ):
22
+ #Signum function
16
23
return 1 if a > 0 else - 1 if a < 0 else 0
17
24
18
25
def clearback ():
26
+
27
+ '''Backspace button function to delete
28
+ a character from the screen'''
29
+
19
30
result1 = ""
20
31
result2 = ""
21
32
global equation
@@ -91,6 +102,12 @@ def clearback():
91
102
except :pass
92
103
93
104
def pressbtn (num ):
105
+
106
+ '''Function to determine which button is pressed.
107
+ Everytime a button a pressed , this function is
108
+ called and a parameter is passed, which updates the
109
+ variables and the expression to be evaluated'''
110
+
94
111
global expression
95
112
global value
96
113
global ans
@@ -137,6 +154,10 @@ def pressbtn(num):
137
154
elif num == 'e^' :value += 'math.e**'
138
155
139
156
def equal ():
157
+
158
+ '''On pressing equal to button, this function is called.
159
+ And it prints the result on the screen.'''
160
+
140
161
global ans
141
162
global value
142
163
global expression
@@ -149,6 +170,7 @@ def equal():
149
170
value = ''
150
171
expression = ''
151
172
173
+ #Configuring the layout of Calculator
152
174
root = Tk ()
153
175
root .title ("Scientific Calculator" )
154
176
@@ -163,18 +185,21 @@ def equal():
163
185
area .insert (0 ,"0" )
164
186
area .grid (row = 0 ,columnspan = 8 )
165
187
188
+ #standard calculator
166
189
def standard ():
167
190
root .geometry ('361x350' )
168
191
area ['width' ]= 28
169
192
area .grid (row = 0 ,columnspan = 4 ,sticky = EW )
170
193
root .title ("Standard Calculator" )
171
-
194
+
195
+ #scientific calculator
172
196
def scientific ():
173
197
root .geometry ('742x350' )
174
198
area ['width' ]= 60
175
199
area .grid (row = 0 ,columnspan = 8 )
176
200
root .title ("Scientific Calculator" )
177
201
202
+ # adding menubar on top
178
203
menubar = Menu (cal )
179
204
filemenu = Menu (menubar ,tearoff = 0 )
180
205
menubar .add_cascade (label = "File" , menu = filemenu )
@@ -187,6 +212,7 @@ def scientific():
187
212
ans = ""
188
213
expression = ""
189
214
215
+ #Fonts and Colours
190
216
font = tkinter .font .Font (size = 12 ,weight = "bold" , family = 'Helvetica' ,)
191
217
h = 2
192
218
w = 7
@@ -200,14 +226,17 @@ def scientific():
200
226
201
227
numberpad = [7 ,8 ,9 ,4 ,5 ,6 ,1 ,2 ,3 ]
202
228
i = 0
229
+
203
230
for j in range (3 ):
204
231
for k in range (3 ):
205
232
Button (cal ,command = lambda x = str (numberpad [i ]) : pressbtn (x ), text = str (numberpad [i ]), bg = bg1 , fg = fg2 ,activebackground = actvbgnd ,
206
233
height = h , width = w ,font = font ).grid (row = j + 2 ,column = k )
207
234
i += 1
208
235
209
- r = 5
210
- c = 7
236
+ r = 5 #number of rows
237
+ c = 7 #number of columns
238
+
239
+ #creating buttons
211
240
Button (cal ,command = lambda : pressbtn (0 ), text = "0" , bg = bg1 , fg = fg2 ,activebackground = actvbgnd ,
212
241
height = h , width = w ,font = font ).grid (row = r ,column = c - 7 )
213
242
Button (cal ,command = lambda : pressbtn ('00' ),text = "00" , bg = bg1 , fg = fg2 ,activebackground = actvbgnd ,
@@ -238,6 +267,7 @@ def scientific():
238
267
height = h ,width = w ,font = font ).grid (row = j + 1 ,column = k + 4 )
239
268
i += 1
240
269
270
+ #set the spacing between buttons
241
271
msize = 60
242
272
cal .rowconfigure (0 ,minsize = 50 )
243
273
for i in range (1 ,6 ):
@@ -247,4 +277,4 @@ def scientific():
247
277
for i in range (8 ):
248
278
cal .columnconfigure (i ,minsize = msize )
249
279
250
- cal .mainloop ()
280
+ cal .mainloop ()
0 commit comments