-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator layout tkinter.py
197 lines (160 loc) · 7.06 KB
/
calculator layout tkinter.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
from tkinter import *
from math import *
class Calculator:
def __init__(self, master):
self.stored_value = None
self.master = master
self.master.title('Calculator')
self.expression = ''
self.display = Text(master, width = 30, height = 3)
self.display.grid(row = 0, column = 0, columnspan = 5, rowspan = 2)
self.create_buttons()
def button_constructor(self, text, command, row, column):
button = Button(self.master, text = text, command = command,bg = 'gray30', fg = 'white', relief = FLAT)
button.grid(row = row, column = column, sticky = N + S + W + E)
return button
def create_buttons(self):
x_squared_button = self.button_constructor('x^2', self.x_squared, 3, 0)
x_cubed_button = self.button_constructor('x^3', self.x_cubed, 3, 1)
x_power_of_n_button = self.button_constructor('x^n', self.x_power_of_n, 3, 2)
off_button = self.button_constructor('OFF', self.clear, 3, 3)
on_button = self.button_constructor('ON', self.clear, 3, 4)
left_button = self.button_constructor('<-', self.left, 4, 0)
right_button = self.button_constructor('->', self.right, 4, 1)
log_button = self.button_constructor('log', self.log, 4, 2)
ln_button = self.button_constructor('ln', self.ln, 4, 3)
sqrt_button = self.button_constructor('sqrt', self.sqrt, 4, 4)
pi_button = self.button_constructor('pi', self.pi, 5, 0)
e_button = self.button_constructor('e', self.e, 5, 1)
arcsin_button = self.button_constructor('asin', self.sin_inverse, 5, 2)
arccos_button = self.button_constructor('acos', self.cos_inverse, 5, 3)
arctan_button = self.button_constructor('atan', self.tan_inverse, 5, 4)
left_bracket_button = self.button_constructor('(', self.left_bracket, 6, 0)
right_bracket_button = self.button_constructor(')', self.right_bracket, 6, 1)
sin_button = self.button_constructor('sin', self.sin, 6, 2)
cos_button = self.button_constructor('cos', self.cos, 6, 3)
tan_button = self.button_constructor('tan', self.tan, 6, 4)
del_button = self.button_constructor('DEL', self.delete, 7, 3)
ac_button = self.button_constructor('AC', self.ac, 7, 4)
times_button = self.button_constructor('X', self.times, 8, 3)
divide_button = self.button_constructor('/', self.divide, 8, 4)
plus_button = self.button_constructor('+', self.plus, 9, 3)
minus_button = self.button_constructor('-', self.minus, 9, 4)
decimal_button = self.button_constructor('.', self.decimal, 10, 1)
clear_button = self.button_constructor('C', self.clear, 10, 2)
ans_button = self.button_constructor('Ans', self.ans, 10, 3)
equal_button = self.button_constructor('=', self.equals, 10, 4)
for i in range(1, 10):
if i <= 3:
row = 9
elif 4 <= i <= 6:
row = 8
else:
row = 7
column = (i % 3) - 1 if ((i%3) - 1) >= 0 else 2
new_button = self.button_constructor(i, lambda i=i: self.print_num(i), row = row, column = column)
zero_button = self.button_constructor(0, lambda i=i: self.print_num(0), row = 10, column = 0)
def print_num(self, number: int):
self.expression += str(number)
self.display.insert(END, number)
def x_squared(self):
self.expression += '** 2'
self.display.insert(END, '^2')
def x_cubed(self):
self.expression += '** 3'
self.display.insert(END, '^3')
def x_power_of_n(self):
self.expression += '**'
self.display.insert(END, '^')
def left(self):
pass
def right(self):
pass
def log(self):
self.expression += 'log10('
self.display.insert(END, 'log(')
def ln(self):
self.expression += 'log('
self.display.insert(END, 'ln(')
def sqrt(self):
self.expression += 'sqrt('
self.display.insert(END, 'sqrt(')
def pi(self):
try:
if int(self.expression[-1]) in range(10):
self.expression += '*pi'
except:
self.expression += 'pi'
self.display.insert(END, 'pi')
def e(self):
try:
if int(self.expression[-1]) in range(10):
self.expression += '*e'
except:
self.expression += 'e'
self.display.insert(END, 'e')
def sin_inverse(self):
self.expression += 'asin('
self.display.insert(END, 'arcsin(')
def cos_inverse(self):
self.expression += 'acos('
self.display.insert(END, 'arccos(')
def tan_inverse(self):
self.expression += 'atan('
self.display.insert(END, 'arctan(')
def left_bracket(self):
self.expression += '('
self.display.insert(END, '(')
def right_bracket(self):
self.expression += ')'
self.display.insert(END, ')')
def sin(self):
self.expression += 'sin('
self.display.insert(END, 'sin(')
def cos(self):
self.expression += 'cos('
self.display.insert(END, 'cos(')
def tan(self):
self.expression += 'tan('
self.display.insert(END, 'tan(')
def delete(self):
self.expression = str(self.expression)[: -1]
current_string = self.display.get(1.0, END)
column_index = len(current_string) - 2
index_to_delete = float('1.'+ str(column_index))
self.display.delete(index_to_delete)
def ac(self):
self.stored_value = eval(self.expression)
self.expression = ''
self.display.delete(0.0, END)
def times(self):
self.expression += '*'
self.display.insert(END, 'x')
def divide(self):
self.expression += '/'
self.display.insert(END, '/')
def plus(self):
self.expression += '+'
self.display.insert(END, '+')
def minus(self):
self.expression += ' - '
self.display.insert(END, '-')
def decimal(self):
self.expression += '.'
self.display.insert(END, '.')
def clear(self):
self.expression = ''
self.display.delete(1.0, END)
def ans(self):
self.expression += str(self.stored_value)
self.display.insert(END, 'Ans')
def equals(self):
print(self.expression)
print(eval(self.expression))
result = str(eval(self.expression))
self.display.delete(0.0, END)
self.display.insert(END, result)
self.expression = result
root = Tk()
main = Calculator(root)
root.mainloop()