-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCalculator GUI.py
More file actions
88 lines (69 loc) · 2.09 KB
/
Calculator GUI.py
File metadata and controls
88 lines (69 loc) · 2.09 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
# __Author__ __Lencof__
# Calculator GUI.py
from tkinter import *
from operator import add, sub, mul, truediv
OPFUNC = {'+': add, '-': sub, '*': mul, '/': truediv}
class ParseErr(Exception):
pass
screen = Tk()
calc = StringVar(screen)
calc.set("")
def lexer(s):
in_int = False
current = []
tokens = []
for cur in s:
in in_int:
if cur.isdigit():
current.append(cur)
continue
else:
tokens.append(int("".join(current)))
in_int = False
current = []
if cur.isdigit():
current.append(cur)
in_int = True
elif cur in ('+', '-', '*', '/'):
tokens.append(cur)
elif not cur.isspace():
return f"Unexpected char '{cur}'"
if in_int:
tokens.append(int("".join(current)))
return tokens
def parser(tokens):
stack = []
op_stack = []
for tok in tokens:
if isinstance(tok, int):
stack.append(tok)
else:
while op_stack and not (tok in ('*', '/') and op_stack[-1] in ('+', '-')):
right = stack.pop()
stack.append(OPFUNC[op_stack.pop()](stack.pop(), right))
op_stack.append(tok)
for op in op_stack[::-1]:
right = stack.pop()
stack.append(OPFUNC[op](stack.pop(), right))
return stack[0]
show = Frame(screen)
entry = Entry(show, textvariable=calc, width="19", state="disable")
entry.grid(column=0, row=0)
ac = Button(show, text='AC', width=4, height=2, command=lambda: calc.set(""))
ac.grid(column=1, row=0)
show.pack()
buttons = Frame(screen)
# figures
tab = ["789/",
"456*",
"123+",
"0.=-"]
for i, line in enumerate(tab):
for j, case in enumerate(line):
if case == "=":
a = lambda x=case: calc.set(parser(lexer(calc.get().strip())))
else:
a = lambda x=case: calc.set(calc.get() + x)
Button(buttons, text=case, width=4, height=2, command=a).grid(column=j, row=i)
buttons.pack() # close
screen.mainloop() # close