-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssessment_1_KivSithVothy.py
More file actions
208 lines (150 loc) · 5.59 KB
/
Assessment_1_KivSithVothy.py
File metadata and controls
208 lines (150 loc) · 5.59 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
from tkinter import *
root = Tk()
root.title("Calculator")
bar = Entry(root, width=30, bd=4,font=("Ubuntu", 18, "normal",),fg="blue", bg="Pale Turquoise", justify=CENTER)
bar.place(x=0, y=0)
X = 85
Y = 90
w = 350
h = 400
# Get the screen dimensions
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
# Find the center of the point
cx = int(sw/2 - h/2)
cy = int(sh/2 - w/2)
root.geometry(f'{w}x{h}+{cx}+{cy}')
root.resizable(False, False)
def insert_number(number):
bar['fg'] = "blue"
text = bar.get()
if (text.split() == []) and (number == "/" or number == "*" or number == ")"):
number = ''
elif (text.endswith('*')) and (number in ("+", "-", "/", "*")):
backspace()
elif (text.endswith('/')) and (number in ("+", "-", "/", "*")):
backspace()
elif (text.endswith('+')) and (number in ("+", "-", "/", "*")):
backspace()
elif (text.endswith('-')) and (number in ("+", "-", "/", "*")):
backspace()
elif (text.endswith('.')) and (number in (".", "+", "-", "/", "*")):
number = ''
bar.insert(END, number)
def backspace():
bar['fg'] = "blue"
try:
text = bar.get()
l = list(text)
l.pop()
text = "".join(l)
bar.delete(0, END)
bar.insert(0, text)
except:
pass
def delete():
bar['fg'] = "blue"
bar.delete(0, END)
def bracket_check():
text = str(bar.get())
Text = list(text)
open_brackets = 0
for char in Text:
if char == "(":
open_brackets += 1
elif char == ")":
open_brackets -= 1
return open_brackets
def evaluate_expression():
text = str(bar.get())
open_brackets = bracket_check()
if open_brackets > 0:
bar.insert(END, ")" * open_brackets)
else:
try:
answer = eval(text)
delete()
bar.insert(0, answer)
bar['fg'] = "forestgreen"
except:
bar['fg'] = "red"
def insert_number_0():
insert_number("0")
def insert_number_1():
insert_number("1")
def insert_number_2():
insert_number("2")
def insert_number_3():
insert_number("3")
def insert_number_4():
insert_number("4")
def insert_number_5():
insert_number("5")
def insert_number_6():
insert_number("6")
def insert_number_7():
insert_number("7")
def insert_number_8():
insert_number("8")
def insert_number_9():
insert_number("9")
# Row 1
clear = Button(root, text="C", font=("Courier New", 16, "bold"),
padx=105, pady=16, bd=4, bg="light cyan", command=delete)
clear.place(x=0, y=Y-50)
plus = Button(root, text="+", font=("Courier New", 16, "bold"), padx=20,
pady=16, bd=4, bg="light cyan", command=lambda: insert_number("+"))
plus.place(x=3 * X, y=Y - 50)
# Row 2
n1 = Button(root, text="1", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=insert_number_1)
n1.place(x=0, y=Y + 20)
n2 = Button(root, text="2", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=insert_number_2)
n2.place(x=X, y=Y + 20)
n3 = Button(root, text="3", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=insert_number_3)
n3.place(x=2 * X, y=Y + 20)
minus = Button(root, text="-", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=lambda: insert_number("-"))
minus.place(x=3 * X, y=Y+20)
# Row 3
n4 = Button(root, text="4", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=insert_number_4)
n4.place(x=0, y=(2 * Y))
n5 = Button(root, text="5", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=insert_number_5)
n5.place(x=X, y=(2 * Y))
n6 = Button(root, text="6", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=insert_number_6)
n6.place(x=2 * X, y=(2 * Y))
multiply = Button(root, text="*", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=lambda: insert_number("*"))
multiply.place(x=3 * X, y=(2 * Y))
# Row 4
n7 = Button(root, text="7", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=insert_number_7)
n7.place(x=0, y=(3 * Y) - 20)
n8 = Button(root, text="8", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=insert_number_8)
n8.place(x=X, y=(3 * Y) - 20)
n9 = Button(root, text="9", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=insert_number_9)
n9.place(x=2 * X, y=(3 * Y) - 20)
divide = Button(root, text="/", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=lambda: insert_number("/"))
divide.place(x=3 * X, y=(3 * Y) - 20)
# Row 5
n0 = Button(root, text="0", font=("Courier New", 16, "bold"), padx=60, pady=16, bd=4, bg="light cyan",
command=insert_number_0)
n0.place(x=0, y=(3 * Y) + 50)
decimal = Button(root, text=".", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=lambda: insert_number("."))
decimal.place(x=2 * X, y=(3 * Y) + 50)
equal = Button(root, text="=", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
command=evaluate_expression)
equal.place(x=3 * X, y=(3 * Y) + 50)
# back = Button(root, text="←", font=("Courier New", 16, "bold"), padx=20, pady=16, bd=4, bg="light cyan",
# command=backspace)
# back.place(x=0, y=(4 * Y) + 20)
root.mainloop()