-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegacy.py
More file actions
221 lines (196 loc) · 11.7 KB
/
Copy pathlegacy.py
File metadata and controls
221 lines (196 loc) · 11.7 KB
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
from tkinter import *
from tkinter.ttk import *
import math
class Calculator:
def __init__(self):
self.root = Tk()
self.root.geometry("322x500")
self.root.resizable(0, 0)
self.root.title("Calculator")
self.root.columnconfigure(0, weight=1)
self.number_pad_buttons()
self.bottom_number_pad()
self.symbols = ["(", ")", "+", "-", chr(247), "x" , "^", "="]
self.special_operators = {"±":"-", chr(247):"/","x":"*", "^":"**"}
self.keys = ["Exit", "Clear", "⌫", "x²", "x³", "10ˣ", "√ˣ", "n!", "log", "sin", "cos", "tan"]
self.top_pad_buttons(self.keys, self.root)
self.functions_button(self.symbols)
self.collect_input = ""
self.user_type_keypad = ""
self.operand = ""
self.style = Style()
self.user_input_display = Entry(master=self.root, state="readonly", font="Verdana 13", width=53, justify="right")
self.inner_entry = Entry(master=self.root, state="readonly", style="style.TEntry", width=53, justify="right")
self.inner_entry.grid(row=1, in_=self.user_input_display)
self.user_input_display.grid(row=1, ipady= 28, rowspan=2, sticky="w")
self.label_text = Label(self.root, font="times 15", text="Scientific", width=32)
self.commandx = ["", "self.clear_display()", "self.back_space()", "self.square_root_2()", "self.cube_root()","self.base_ten_root()",
"self.square_root()", "self.factorial()", "self.log()", "self.sin()", "self.cos()", "self.tan()"]
self.style.configure("Tlabel", fg="white", bg="black")
self.label_text.grid(row=0, ipady="10", columnspan=3, column=0, sticky="w")
self.style.configure(".", foreground ="#900763", background="blue")
def top_pad_buttons(self, keys, root):
index = 0
frame = Frame()
frame.grid(row=3, rowspan=3, sticky="nw")
for x in range(3,7):
for o in range(3, 6):
button = Button(frame, text=keys[index], width=13, command= self.root.destroy if index == 0 else lambda x=index:eval(self.commandx[x]))
button.grid(row=x, column=o, ipady=11)
index+=1
def number_pad_buttons(self):
count = 1
frame = Frame()
frame.grid(sticky="nw", row=6, rowspan=9)
for x in range(4, 7):
for o in range(4, 7):
button = Button(frame, text=count, width=13, style="TButton", command=lambda x=count:self.user_input(x))
button.grid(row=x, column=o, ipady=11)
count+=1
def functions_button(self, lists):
frame = Frame()
frame.grid(rowspan=len(lists), row=3, sticky="ee")
row_count = 3
for x in range(len(lists)):
button = Button(frame, text=lists[x], width=8, command=(lambda:self.equal_to()) if x == len(lists)-1 else lambda x= x:self.user_input(lists[x]))
button.grid(row=row_count, ipady=11)
row_count += 1
def bottom_number_pad(self):
signs = ["±", "0", "."]
frame = Frame()
frame.grid(row=10, rowspan=2, sticky="nw")
for o, x in enumerate(signs):
button = Button(frame, text=x, width=13, command=lambda x= o:self.user_input(signs[x]))
button.grid(row=0, column=o, ipady=11)
def user_input(self, key):
if key in self.symbols:
self.inner_entry.configure(state="active")
self.collect_input = self.special_operators.get(str(key), str(key))
self.operand += self.collect_input
self.inner_entry.insert(END, self.operand)
self.inner_entry.configure(state="readonly")
self.operand = ""
self.user_input_display.configure(state="active")
self.user_input_display.delete(0, END)
self.user_input_display.configure(state="readonly")
else:
self.collect_input = self.special_operators.get(str(key), str(key))
self.user_type_keypad += self.collect_input
self.operand += self.collect_input
self.user_input_display.configure(state="active")
self.user_input_display.insert(END, self.collect_input)
self.user_input_display.configure(state="readonly")
print(self.collect_input)
return self.user_type_keypad
def clear_display(self):
self.user_input_display.configure(state="active")
self.user_input_display.delete(0, END)
self.user_input_display.configure(state="readonly")
self.inner_entry.configure(state="active")
self.inner_entry.delete(0, END)
self.inner_entry.configure(state="readonly")
def back_space(self):
self.user_input_display.configure(state="active")
self.user_input_display.delete(len(self.user_input_display.get())-1)
self.user_input_display.configure(state="readonly")
def factorial(self):
try:
if float(self.user_input_display.get()) > 3000:
evaluate = "Overflow!"
else:
evaluate = math.factorial(int(self.user_input_display.get()))
except Exception :
evaluate = "Invalid Expression!"
self.user_input_display.configure(state="active")
self.user_input_display.delete(0, END)
self.user_input_display.insert(END, evaluate)
self.user_input_display.configure(state="readonly")
def square_root(self):
try:
evaluate = int(float(self.user_input_display.get()))**0.5
except Exception:
evaluate = "Invalid Expression!"
self.user_input_display.configure(state="active")
self.user_input_display.delete(0, END)
self.user_input_display.insert(END, evaluate)
self.user_input_display.configure(state="readonly")
def log(self):
try:
evaluate = math.log10(float(self.user_input_display.get()))
except Exception:
evaluate = "Invalid Expression!"
self.user_input_display.configure(state="active")
self.user_input_display.delete(0, END)
self.user_input_display.insert(END, evaluate)
self.user_input_display.configure(state="readonly")
def sin(self):
try:
evaluate = math.sin(float(self.user_input_display.get()))
except Exception:
evaluate = "Invalid Expression!"
self.user_input_display.configure(state="active")
self.user_input_display.delete(0, END)
self.user_input_display.insert(END, evaluate)
self.user_input_display.configure(state="readonly")
def cos(self):
try:
evaluate = math.cos(float(self.user_input_display.get()))
except Exception:
evaluate = "Invalid Expression!"
self.user_input_display.configure(state="active")
self.user_input_display.delete(0, END)
self.user_input_display.insert(END, evaluate)
self.user_input_display.configure(state="readonly")
def tan(self):
try:
evaluate = math.tan(float(self.user_input_display.get()))
except Exception:
evaluate = "Invalid Expression!"
self.user_input_display.configure(state="active")
self.user_input_display.delete(0, END)
self.user_input_display.insert(END, evaluate)
self.user_input_display.configure(state="readonly")
def square_root_2(self):
try:
evaluate = float(self.user_input_display.get()) **2
except Exception:
evaluate = "Invalid Expression!"
self.user_input_display.configure(state="active")
self.user_input_display.delete(0, END)
self.user_input_display.insert(END, evaluate)
self.user_input_display.configure(state="readonly")
def base_ten_root(self):
try:
evaluate = 10**(float(self.user_input_display.get()))
except Exception:
evaluate = "Invalid Expression!"
self.user_input_display.configure(state="active")
self.user_input_display.delete(0, END)
self.user_input_display.insert(END, evaluate)
self.user_input_display.configure(state="readonly")
def cube_root(self):
try:
evaluate = (float(self.user_input_display.get())) **3
except Exception:
evaluate = "Invalid Expression!"
self.user_input_display.configure(state="active")
self.user_input_display.delete(0, END)
self.user_input_display.insert(END, evaluate)
self.user_input_display.configure(state="readonly")
def equal_to(self):
try:
evaluate = eval(str(self.inner_entry.get()+self.user_input_display.get()))
except Exception as error:
evaluate = f"Invalid Expression! {error}"
self.user_input_display.configure(state="active")
self.user_input_display.delete(0, END)
self.user_input_display.insert(END, evaluate)
self.user_input_display.configure(state="readonly")
self.inner_entry.configure(state="active")
self.inner_entry.delete(0, END)
self.inner_entry.configure(state="readonly")
def run(self):
self.root.mainloop()
if __name__ == "__main__":
cal = Calculator()
cal.run()