-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator_1.0.py
309 lines (291 loc) · 8.44 KB
/
Calculator_1.0.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import(
QApplication,
QWidget,
QPushButton,
QLabel,
QVBoxLayout,
QHBoxLayout )
app = QApplication([])
window = QWidget()
window.setFixedSize(360, 500)
window.setWindowTitle("Калькулятор")
window.setStyleSheet('''
*{
font-size: 24px;
}
QWidget{
background-color: rgba(0,0,0,0.5);
}
QPushButton{
height: 70px;
color: white;
}''')
main_layout = QVBoxLayout()
main_layout.setSpacing(0)
h1_layout = QHBoxLayout()
h1_layout.setSpacing(12)
h2_layout = QHBoxLayout()
h2_layout.setSpacing(12)
h3_layout = QHBoxLayout()
h3_layout.setSpacing(12)
h4_layout = QHBoxLayout()
h4_layout.setSpacing(12)
h5_layout = QHBoxLayout()
h5_layout.setSpacing(12)
h6_layout = QHBoxLayout()
h6_layout.setSpacing(12)
total = QLabel('')
total_str = ''
h1_layout.addWidget(total, alignment= Qt.AlignRight)
total.setStyleSheet('''background-color: transparent;
font-size: 28px;
color: white;
''')
button_C = QPushButton('C')
button_AC = QPushButton('AC')
button_percent = QPushButton('%')
button_devision = QPushButton('/')
button_multi = QPushButton('*') # 2
button_minus = QPushButton('-') # 3
button_plus = QPushButton('+') # 4
button_dot = QPushButton('.') # 6
button_equals = QPushButton('=') # 6
button_devision.setStyleSheet('''background-color: #EBAD1C;''')
button_multi.setStyleSheet('''background-color: #EBAD1C;''')
button_minus.setStyleSheet('''background-color: #EBAD1C;''')
button_plus.setStyleSheet('''background-color: #EBAD1C;''')
button_equals.setStyleSheet('''background-color: #EBAD1C;''')
button_dot.setStyleSheet('''background-color: #7D7D7D;''')
h2_layout.addWidget(button_C)
h2_layout.addWidget(button_AC)
h2_layout.addWidget(button_percent)
h2_layout.addWidget(button_devision)
numbers = []
for number in range(10):
numbers.append(QPushButton(str(number)))
numbers[-1].setStyleSheet('''
background-color: #7D7D7D; ''')
numbers[0].setStyleSheet('''width: 152px;
background-color: #7D7D7D;''')
k = 1
h6_layout.addWidget(numbers[0])
for i in range(3):
h5_layout.addWidget(numbers[k])
k += 1
for i in range(3):
h4_layout.addWidget(numbers[k])
k += 1
for i in range(3):
h3_layout.addWidget(numbers[k])
k += 1
h3_layout.addWidget(button_multi)
h4_layout.addWidget(button_minus)
h5_layout.addWidget(button_plus)
h6_layout.addWidget(button_dot)
h6_layout.addWidget(button_equals)
main_layout.addLayout(h1_layout)
main_layout.addLayout(h2_layout)
main_layout.addLayout(h3_layout)
main_layout.addLayout(h4_layout)
main_layout.addLayout(h5_layout)
main_layout.addLayout(h6_layout)
def a1():
global total_str
total_str += '1'
total.setText(total_str)
def a2():
global total_str
total_str += '2'
total.setText(total_str)
def a3():
global total_str
total_str += '3'
total.setText(total_str)
def a4():
global total_str
total_str += '4'
total.setText(total_str)
def a5():
global total_str
total_str += '5'
total.setText(total_str)
def a6():
global total_str
total_str += '6'
total.setText(total_str)
def a7():
global total_str
total_str += '7'
total.setText(total_str)
def a8():
global total_str
total_str += '8'
total.setText(total_str)
def a9():
global total_str
total_str += '9'
total.setText(total_str)
def a0():
global total_str
total_str += '0'
total.setText(total_str)
blocked = ['-', '+', '.', '*', '/']
def a_plus():
global total_str
if total_str != "":
if total_str[-1] not in blocked:
total_str += '+'
total.setText(total_str)
def a_minus():
global total_str
if len(total_str) >= 2:
if total_str[-1] != '.':
if total_str[-1] in blocked and total_str[-2] in blocked:
pass
else:
total_str += '-'
total.setText(total_str)
else:
if total_str != '-':
total_str += '-'
total.setText(total_str)
def a_devision():
global total_str
if total_str != "":
if total_str[-1] not in blocked:
total_str += '/'
total.setText(total_str)
def a_multi():
global total_str
if total_str != "":
if total_str[-1] not in blocked:
total_str += '*'
total.setText(total_str)
def a_dot():
global total_str
if total_str != "":
if total_str[-1] not in blocked and total_str[-1] != "%":
total_str += '.'
total.setText(total_str)
def a_percent():
global total_str
if total_str != "":
if total_str[-1] not in blocked and total_str[-1] != "%":
total_str += '%'
total.setText(total_str)
def a_AC():
global total_str
total.clear()
total_str = ''
def a_C():
global total_str
if total_str:
tmp_list = list(total_str)
tmp_str = ''
del tmp_list[-1]
for i in tmp_list:
tmp_str += i
total_str = tmp_str
total.setText(total_str)
defs_numbers = {}
defs_numbers['1'] = a1
defs_numbers['2'] = a2
defs_numbers['3'] = a3
defs_numbers['4'] = a4
defs_numbers['5'] = a5
defs_numbers['6'] = a6
defs_numbers['7'] = a7
defs_numbers['8'] = a8
defs_numbers['9'] = a9
defs_numbers['0'] = a0
for i in range(0, 10):
numbers[i].clicked.connect(defs_numbers[str(i)])
button_plus.clicked.connect(a_plus)
button_minus.clicked.connect(a_minus)
button_devision.clicked.connect(a_devision)
button_multi.clicked.connect(a_multi)
button_dot.clicked.connect(a_dot)
button_percent.clicked.connect(a_percent)
button_C.clicked.connect(a_C)
button_AC.clicked.connect(a_AC)
def a_equals():
global total_str
if total_str[-1] in blocked:
a_C()
total_int = 0
total_list = []
# 100+200*10
count_symbols = 0
for i in range(0,len(total_str)):
if (total_str[i] == '+' or
total_str[i] == '-' or
total_str[i] == '*' or
total_str[i] == '/' or
total_str[i] == '%'):
if count_symbols != 0:
tmp = total_str[i-count_symbols:i]
try:
total_list.append(int(tmp))
except:
total_list.append(float(tmp))
total_list.append(total_str[i])
count_symbols = 0
elif total_str[i-1] == '%':
total_list.append(total_str[i])
count_symbols = 0
else:
count_symbols += 1
elif i == len(total_str) - 1:
tmp = total_str[i-count_symbols:i+1]
try:
total_list.append(int(tmp))
except:
total_list.append(float(tmp))
else:
count_symbols += 1
print(total_list)
while len(total_list) != 1:
for i in range(0, len(total_list)):
if total_list[i] == "%":
# 81% - 100 + 200
# 100 - 50 %
# i-3 i-2 i-1 i
if i == 1:
total_list[0]= total_list[0]/100
del total_list[1]
break
else:
total_list[i-1] = total_list[i-3]/100*total_list[i-1]
print(total_list[i-1])
del total_list[i]
break
elif total_list[i] == "*":
total_list[i] = total_list[i-1] * total_list[i+1]
del total_list[i+1]
del total_list[i-1]
break
elif total_list[i] == "/":
total_list[i] = total_list[i-1] / total_list[i+1]
del total_list[i+1]
del total_list[i-1]
break
elif ('*' not in total_list and
"/" not in total_list and
'%' not in total_list):
if total_list[i] == "+":
total_list[i] = total_list[i-1] + total_list[i+1]
del total_list[i+1]
del total_list[i-1]
break
elif total_list[i] == "-":
total_list[i] = total_list[i-1] - total_list[i+1]
del total_list[i+1]
del total_list[i-1]
break
total.setText(str(total_list[0]))
total_str = str(total_list[0])
button_equals.clicked.connect(a_equals)
window.setLayout(main_layout)
window.show()
app.exec_()