-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculator.py
334 lines (288 loc) · 9.15 KB
/
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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# -*- coding: utf-8 -*-
"""
Module implementing MyCalc.
"""
from PyQt4.QtCore import pyqtSlot
from PyQt4.QtGui import QDialog
from UI.Ui_Calc_dialog import Ui_Dialog
class MyCalc(QDialog, Ui_Dialog):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
"""
Constructor
@param parent reference to the parent widget (QWidget)
"""
super(MyCalc, self).__init__()
self.setupUi(self)
# 定义变量
self.str1 = '' # 第一个操作数
self.str2 = '' # 第二个操作数
self.flag = 0 # 输入数字标志,0表示未完成,1表示操作数输入完成,2计算后结果作为第一个操作数
self.op = '' # 操作符
@pyqtSlot() # 信号槽方法以响应事件,每个响应前面加这个修饰
def on_pushButton_0_clicked(self): # on_对象名称_信号名称(self,参数):也可以自己按此格式创建
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 0:
self.str1 = self.str1 + '0'
self.lineEdit_result.setText(self.str1)
else:
self.str2 = self.str2 + '0'
self.lineEdit_result.setText(self.str2)
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_1_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 0:
self.str1 = self.str1 + '1'
self.lineEdit_result.setText(self.str1)
else:
self.str2 = self.str2 + '1'
self.lineEdit_result.setText(self.str2)
# raise NotImplementedError
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_2_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 0:
self.str1 = self.str1 + '2'
self.lineEdit_result.setText(self.str1)
else:
self.str2 = self.str2 + '2'
self.lineEdit_result.setText(self.str2)
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_add_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 2:
self.str1 = self.lineEdit_result.text()
self.str2 = ''
self.flag = 1
self.op = '+'
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_3_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 0:
self.str1 = self.str1 + '3'
self.lineEdit_result.setText(self.str1)
else:
self.str2 = self.str2 + '3'
self.lineEdit_result.setText(self.str2)
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_4_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 0:
self.str1 = self.str1 + '4'
self.lineEdit_result.setText(self.str1)
else:
self.str2 = self.str2 + '4'
self.lineEdit_result.setText(self.str2)
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_5_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 0:
self.str1 = self.str1 + '5'
self.lineEdit_result.setText(self.str1)
else:
self.str2 = self.str2 + '5'
self.lineEdit_result.setText(self.str2)
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_sub_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 2:
self.str1 = self.lineEdit_result.text()
self.str2 = ''
self.flag = 1
self.op = '-'
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_6_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 0:
self.str1 = self.str1 + '6'
self.lineEdit_result.setText(self.str1)
else:
self.str2 = self.str2 + '6'
self.lineEdit_result.setText(self.str2)
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_7_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 0:
self.str1 = self.str1 + '7'
self.lineEdit_result.setText(self.str1)
else:
self.str2 = self.str2 + '7'
self.lineEdit_result.setText(self.str2)
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_8_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 0:
self.str1 = self.str1 + '8'
self.lineEdit_result.setText(self.str1)
else:
self.str2 = self.str2 + '8'
self.lineEdit_result.setText(self.str2)
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_mult_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 2:
self.str1 = self.lineEdit_result.text()
self.str2 = ''
self.flag = 1
self.op = '*'
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_9_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 0:
self.str1 = self.str1 + '9'
self.lineEdit_result.setText(self.str1)
else:
self.str2 = self.str2 + '9'
self.lineEdit_result.setText(self.str2)
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_dot_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
strx = self.lineEdit_result.text()
if strx.find('.') == -1: # 如果没有小数点就加个小数点
if self.flag == 0:
self.str1 = self.str1 + '.'
self.lineEdit_result.setText(self.str1)
elif self.flag == 1:
self.str2 = self.str2 + '.'
self.lineEdit_result.setText(self.str2)
if strx == '':
if self.flag == 0:
self.str1 = '0.'
self.lineEdit_result.setText(self.str1)
elif self.flag == 1:
self.str2 = '0.'
self.lineEdit_result.setText(self.str2)
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_reverse_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
strx = self.lineEdit_result.text()
fla = -float(strx)
strx = str(fla)
self.lineEdit_result.setText(strx)
if self.flag == 1:
self.str2 = strx
else:
self.str1 = strx
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_div_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.flag == 2:
self.str1 = self.lineEdit_result.text()
self.str2 = ''
self.flag = 1
self.op = '/'
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_clear_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
self.flag == 0
self.str1 = ''
self.str2 = ''
self.lineEdit_result.setText(self.str1)
# raise NotImplementedError
@pyqtSlot()
def on_pushButton_calc_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.str1 == '':
a = 0
else:
a = float(self.str1)
if self.str2 == '':
b = 0
else:
b = float(self.str2)
res = 0
if self.op == '+':
res = a + b
elif self.op == '-':
res = a - b
elif self.op == '*':
res = a * b
elif self.op == '/':
if b == 0:
res = 0
else:
res = a / b
self.lineEdit_result.setText(str(res))
self.flag = 2
# raise NotImplementedError
#
# if __name__ == "__main__":
# import sys
# from PyQt4 import QtGui
#
# app = QtGui.QApplication(sys.argv)
# Dialog = MyCalc() # 运行计算器窗口
# Dialog.show() # 显示窗口
# sys.exit(app.exec_())