-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
263 lines (234 loc) · 10.6 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# Arnab Adhikary
from tkinter import *
import math
import numpy as np
# Function to add in the entry of text display
def button_click(char):
global calc_operator
calc_operator += str(char)
text_input.set(calc_operator)
# Function to clear the whole entry of text display
def button_clear_all():
global calc_operator
calc_operator = ""
text_input.set("")
# Function to delete one by one from the last in the entry of text display
def button_delete():
global calc_operator
text = calc_operator[:-1]
calc_operator = text
text_input.set(text)
# Function to calculate the factorial of a number
def factorial(n):
if n==0 or n==1:
return 1
else:
return n*factorial(n-1)
def fact_func():
global calc_operator
result = str(factorial(int(calc_operator)))
calc_operator = result
text_input.set(result)
# Function to calculate trigonometric numbers of an angle
def trig_sin():
global calc_operator
result = str(math.sin(math.radians(int(calc_operator))))
calc_operator = result
text_input.set(result)
def trig_cos():
global calc_operator
result = str(math.cos(math.radians(int(calc_operator))))
calc_operator = result
text_input.set(result)
def trig_tan():
global calc_operator
result = str(math.tan(math.radians(int(calc_operator))))
calc_operator = result
text_input.set(result)
def trig_cot():
global calc_operator
result = str(1/math.tan(math.radians(int(calc_operator))))
calc_operator = result
text_input.set(result)
# Function to find the square root of a number
def square_root():
global calc_operator
if int(calc_operator)>=0:
temp = str(eval(calc_operator+'**(1/2)'))
calc_operator = temp
else:
temp = "ERROR"
text_input.set(temp)
# Function to find the third root of a number
def third_root():
global calc_operator
if int(calc_operator)>=0:
temp = str(eval(calc_operator+'**(1/3)'))
calc_operator = temp
else:
temp = "ERROR"
text_input.set(temp)
# Function to change the sign of number
def sign_change():
global calc_operator
if calc_operator[0]=='-':
temp = calc_operator[1:]
else:
temp = '-'+calc_operator
calc_operator = temp
text_input.set(temp)
# Function to calculate the percentage of a number
def percent():
global calc_operator
temp = str(eval(calc_operator+'/100'))
calc_operator = temp
text_input.set(temp)
# Funtion to find the result of an operation
def button_equal():
global calc_operator
temp_op = str(eval(calc_operator))
text_input.set(temp_op)
calc_operator = temp_op
'''
Variables
'''
sin, cos, tan = math.sin, math.cos, math.tan
log, ln = math.log10, math.log
e = math.exp
p = math.pi
E = '*10**'
tk_calc = Tk()
tk_calc.configure(bg="#293C4A", bd=10)
tk_calc.title("Scientific Calculator")
calc_operator = ""
text_input = StringVar()
text_display = Entry(tk_calc, font=('sans-serif', 20, 'bold'), textvariable=text_input,
bd=5, insertwidth = 5, bg='#BBB', justify='right').grid(columnspan=5, padx = 10, pady = 15)
button_params = {'bd':5, 'fg':'#BBB', 'bg':'#3C3636', 'font':('sans-serif', 20, 'bold')}
button_params_main = {'bd':5, 'fg':'#000', 'bg':'#BBB', 'font':('sans-serif', 20, 'bold')}
'''
Buttons
'''
#--1st row--
# Absolute value of a number
abs_value = Button(tk_calc, button_params, text='abs',
command=lambda:button_click('abs(')).grid(row=1, column=0, sticky="nsew")
# Remainder of a division
modulo = Button(tk_calc, button_params, text='mod',
command=lambda:button_click('%')).grid(row=1, column=1, sticky="nsew")
# Integer division quotient
int_div = Button(tk_calc, button_params, text='div',
command=lambda:button_click('//')).grid(row=1, column=2, sticky="nsew")
# Factorial of a number
factorial_button = Button(tk_calc, button_params, text='x!',
command=fact_func).grid(row=1, column=3, sticky="nsew")
# Euler's number e
eulers_num = Button(tk_calc, button_params, text='e',
command=lambda:button_click(str(math.exp(1)))).grid(row=1, column=4, sticky="nsew")
#--2nd row--
# Sine of an angle in degrees
sine = Button(tk_calc, button_params, text='sin',
command=trig_sin).grid(row=2, column=0, sticky="nsew")
# Cosine of an angle in degrees
cosine = Button(tk_calc, button_params, text='cos',
command=trig_cos).grid(row=2, column=1, sticky="nsew")
# Tangent of an angle in degrees
tangent = Button(tk_calc, button_params, text='tan',
command=trig_tan).grid(row=2, column=2, sticky="nsew")
# Cotangent of an angle in degrees
cotangent = Button(tk_calc, button_params, text='cot',
command=trig_cot).grid(row=2, column=3, sticky="nsew")
# Pi(3.14...) number
pi_num = Button(tk_calc, button_params, text='π',
command=lambda:button_click(str(math.pi))).grid(row=2, column=4, sticky="nsew")
#--3rd row--
# Power of 2
second_power = Button(tk_calc, button_params, text='x\u00B2',
command=lambda:button_click('**2')).grid(row=3, column=0, sticky="nsew")
# Power of 3
third_power = Button(tk_calc, button_params, text='x\u00B3',
command=lambda:button_click('**3')).grid(row=3, column=1, sticky="nsew")
# Power of n
nth_power = Button(tk_calc, button_params, text='x^n',
command=lambda:button_click('**')).grid(row=3, column=2, sticky="nsew")
# Inverse number
inv_power = Button(tk_calc, button_params, text='x\u207b\xb9',
command=lambda:button_click('**(-1)')).grid(row=3, column=3, sticky="nsew")
# Powers of 10
tens_powers = Button(tk_calc, button_params, text='10^x', font=('sans-serif', 15, 'bold'),
command=lambda:button_click('10**')).grid(row=3, column=4, sticky="nsew")
#--4th row--
# Square root of a number
square_root = Button(tk_calc, button_params, text='\u00B2\u221A',
command=square_root).grid(row=4, column=0, sticky="nsew")
# Third root of a number
third_root = Button(tk_calc, button_params, text='\u00B3\u221A',
command=third_root).grid(row=4, column=1, sticky="nsew")
# nth root of a number
nth_root = Button(tk_calc, button_params, text='\u221A',
command=lambda:button_click('**(1/')).grid(row=4, column=2, sticky="nsew")
# Logarithm of a number with base 10
log_base10 = Button(tk_calc, button_params, text='log\u2081\u2080', font=('sans-serif', 16, 'bold'),
command=lambda:button_click('log(')).grid(row=4, column=3, sticky="nsew")
# Logarithm of a number with base e (ln)
log_basee = Button(tk_calc, button_params, text='ln',
command=lambda:button_click('ln(')).grid(row=4, column=4, sticky="nsew")
#--5th row--
# Add a left parentheses
left_par = Button(tk_calc, button_params, text='(',
command=lambda:button_click('(')).grid(row=5, column=0, sticky="nsew")
# Add a right parentheses
right_par = Button(tk_calc, button_params, text=')',
command=lambda:button_click(')')).grid(row=5, column=1, sticky="nsew")
# Change the sign of a number
signs = Button(tk_calc, button_params, text='\u00B1',
command=sign_change).grid(row=5, column=2, sticky="nsew")
# Transform number to percentage
percentage = Button(tk_calc, button_params, text='%',
command=percent).grid(row=5, column=3, sticky="nsew")
# Calculate the function e^x
ex = Button(tk_calc, button_params, text='e^x',
command=lambda:button_click('e(')).grid(row=5, column=4, sticky="nsew")
#--6th row--
button_7 = Button(tk_calc, button_params_main, text='7',
command=lambda:button_click('7')).grid(row=6, column=0, sticky="nsew")
button_8 = Button(tk_calc, button_params_main, text='8',
command=lambda:button_click('8')).grid(row=6, column=1, sticky="nsew")
button_9 = Button(tk_calc, button_params_main, text='9',
command=lambda:button_click('9')).grid(row=6, column=2, sticky="nsew")
delete_one = Button(tk_calc, bd=5, fg='#000', font=('sans-serif', 20, 'bold'),
text='DEL', command=button_delete, bg='#db701f').grid(row=6, column=3, sticky="nsew")
delete_all = Button(tk_calc, bd=5, fg='#000', font=('sans-serif', 20, 'bold'),
text='AC', command=button_clear_all, bg='#db701f').grid(row=6, column=4, sticky="nsew")
#--7th row--
button_4 = Button(tk_calc, button_params_main, text='4',
command=lambda:button_click('4')).grid(row=7, column=0, sticky="nsew")
button_5 = Button(tk_calc, button_params_main, text='5',
command=lambda:button_click('5')).grid(row=7, column=1, sticky="nsew")
button_6 = Button(tk_calc, button_params_main, text='6',
command=lambda:button_click('6')).grid(row=7, column=2, sticky="nsew")
mul = Button(tk_calc, button_params_main, text='*',
command=lambda:button_click('*')).grid(row=7, column=3, sticky="nsew")
div = Button(tk_calc, button_params_main, text='/',
command=lambda:button_click('/')).grid(row=7, column=4, sticky="nsew")
#--8th row--
button_1 = Button(tk_calc, button_params_main, text='1',
command=lambda:button_click('1')).grid(row=8, column=0, sticky="nsew")
button_2 = Button(tk_calc, button_params_main, text='2',
command=lambda:button_click('2')).grid(row=8, column=1, sticky="nsew")
button_3 = Button(tk_calc, button_params_main, text='3',
command=lambda:button_click('3')).grid(row=8, column=2, sticky="nsew")
add = Button(tk_calc, button_params_main, text='+',
command=lambda:button_click('+')).grid(row=8, column=3, sticky="nsew")
sub = Button(tk_calc, button_params_main, text='-',
command=lambda:button_click('-')).grid(row=8, column=4, sticky="nsew")
#--9th row--
button_0 = Button(tk_calc, button_params_main, text='0',
command=lambda:button_click('0')).grid(row=9, column=0, sticky="nsew")
point = Button(tk_calc, button_params_main, text='.',
command=lambda:button_click('.')).grid(row=9, column=1, sticky="nsew")
exp = Button(tk_calc, button_params_main, text='EXP', font=('sans-serif', 16, 'bold'),
command=lambda:button_click(E)).grid(row=9, column=2, sticky="nsew")
equal = Button(tk_calc, button_params_main, text='=',
command=button_equal).grid(row=9, columnspan=2, column=3, sticky="nsew")
tk_calc.mainloop()