-
Notifications
You must be signed in to change notification settings - Fork 0
/
my lovely calculator.py
125 lines (92 loc) · 2.74 KB
/
my lovely 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
from tkinter import *
from functools import partial
root = Tk()
root.geometry("200x235")
root.title("Calculator")
standby = True
edited = False
Continue = False
lastResult = ""
lastEquation = ""
lbl = Label(root, text="0", bg="white", width=50, height=2)
lbl.pack()
signs = ["÷", "×", "-", "+"]
replace_signs = {"÷": "/", "×": "*"}
def calculate(equation):
for key, val in replace_signs.items():
equation = equation.replace(key, val)
return eval(equation)
def getLast(equation):
for sign in signs:
equation = equation.replace(sign, "," + sign)
sepEquaList = equation.split(",")
return sepEquaList[len(sepEquaList) - 1]
def onClick(text):
global standby
global lastResult
global lastEquation
global Continue
if standby:
if str(text) == "=" and lbl.cget("text") == "0":
return
if str(text) in signs:
Continue = True
else:
if not Continue:
lbl.config(text="")
standby = False
# edited = True
if str(text) == "=":
Continue = False
standby = True
try:
equation = lbl.cget("text")
result = calculate(equation)
lbl.config(text=result)
lastEquation = equation
lastResult = result
except:
try:
print(lastResult)
print(lastEquation)
result = calculate(str(lastResult) + str(getLast(lastEquation)))
lbl.config(text=result)
lastResult = result
except Exception as err:
print(str(err))
lbl.config(text="Error")
elif str(text) == "🗑":
Continue = False
standby = True
lastResult = ""
lastEquation = ""
lbl.config(text="0")
else:
lbl.config(text=str(lbl.cget("text")) + str(text))
def newButton(text, x, y, w=0):
return Button(root, text=text, width=w, command=partial(onClick, text)).place(
x=x, y=y
)
def newButtonRow(texts, row):
if len(texts) > 2:
for index, text in enumerate(texts):
newButton(
text,
((10 + ((index + 1) * 40)) - 40)
+ (0 if index + 1 != len(texts) else 10),
(45 + (row * 30)) - 30,
)
else:
for index, text in enumerate(texts):
newButton(
text,
(10 + ((index + 1) * 130)) - 130,
(45 + (row * 30)) - 30,
10 if index == 0 else 0,
)
newButtonRow([7, 8, 9, "÷"], 1)
newButtonRow([4, 5, 6, "×"], 2)
newButtonRow([1, 2, 3, "-"], 3)
newButtonRow([0, "+"], 4)
newButtonRow(["=", "🗑"], 5)
root.mainloop()