-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUITest.py
267 lines (228 loc) · 7.11 KB
/
GUITest.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
#GUITest.py
#Started off as a way of testing PyQt5, now is a functioning graphical application calculator
#Manoj K, 11/03/2021
from PyQt5.QtWidgets import QApplication, QLabel, QGridLayout, QWidget, QPushButton, QMessageBox
def button1Pressed(): #Need to find way to read button string so that one func can be used for all
global isFirstNumber
global firstNumber
global secondNumber
global displayLabel = QLabel('1')
if(isFirstNumber):
firstNumber = firstNumber + '1'
else:
secondNumber = secondNumber + '1'
def button2Pressed():
global isFirstNumber
global firstNumber
global secondNumber
global displayLabel = QLabel('2')
if(isFirstNumber):
firstNumber = firstNumber + '2'
else:
secondNumber = secondNumber + '2'
def button3Pressed():
global isFirstNumber
global firstNumber
global secondNumber
global displayLabel = QLabel('3')
if(isFirstNumber):
firstNumber = firstNumber + '3'
else:
secondNumber = secondNumber + '3'
def button4Pressed():
global isFirstNumber
global firstNumber
global secondNumber
global displayLabel = QLabel('4')
if(isFirstNumber):
firstNumber = firstNumber + '4'
else:
secondNumber = secondNumber + '4'
def button5Pressed():
global isFirstNumber
global firstNumber
global secondNumber
global displayLabel = QLabel('5')
if(isFirstNumber):
firstNumber = firstNumber + '5'
else:
secondNumber = secondNumber + '5'
def button6Pressed():
global isFirstNumber
global firstNumber
global secondNumber
global displayLabel = QLabel('6')
if(isFirstNumber):
firstNumber = firstNumber + '6'
else:
secondNumber = secondNumber + '6'
def button7Pressed():
global isFirstNumber
global firstNumber
global secondNumber
global displayLabel = QLabel('7')
if(isFirstNumber):
firstNumber = firstNumber + '7'
else:
secondNumber = secondNumber + '7'
def button8Pressed():
global isFirstNumber
global firstNumber
global secondNumber
global displayLabel = QLabel('8')
if(isFirstNumber):
firstNumber = firstNumber + '8'
else:
secondNumber = secondNumber + '8'
def button9Pressed():
global isFirstNumber
global firstNumber
global secondNumber
global displayLabel = QLabel('9')
if(isFirstNumber):
firstNumber = firstNumber + '9'
else:
secondNumber = secondNumber + '9'
def button0Pressed():
global isFirstNumber
global firstNumber
global secondNumber
global displayLabel = QLabel('0') #INstead update QLabel with firstNumber/secondNumber to display full number
if(isFirstNumber):
firstNumber = firstNumber + '0'
else:
secondNumber = secondNumber + '0'
def buttonPlusPressed():
global isFirstNumber
global firstNumber
global secondNumber
global calcType
if(isFirstNumber):
calcType = '+'
isFirstNumber = False
else:
alert = QMessageBox()
alert.setText('You have already selected a function')
alert.exec()
def buttonMinusPressed():
global isFirstNumber
global firstNumber
global secondNumber
global calcType
if(isFirstNumber):
calcType = '-'
isFirstNumber = False
else:
alert = QMessageBox()
alert.setText('You have already selected a function')
alert.exec()
def buttonMultPressed():
global isFirstNumber
global firstNumber
global secondNumber
global calcType
if(isFirstNumber):
calcType = '*'
isFirstNumber = False
else:
alert = QMessageBox()
alert.setText('You have already selected a function')
alert.exec()
def buttonDivPressed():
global isFirstNumber
global firstNumber
global secondNumber
global calcType
if(isFirstNumber):
calcType = '/'
isFirstNumber = False
else:
alert = QMessageBox()
alert.setText('You have already selected a function')
alert.exec()
def buttonEqualPressed():
global isFirstNumber
global firstNumber
global secondNumber
global calcType
if(~isFirstNumber): #Add textbox with input number/answer
firstNumber = int(firstNumber)
secondNumber = int(secondNumber)
if calcType == '+':
output = firstNumber + secondNumber
elif calcType == '-':
output = firstNumber - secondNumber #Replace with switchcase
elif calcType == '*':
output = firstNumber * secondNumber
elif calcType == '/':
output = firstNumber / secondNumber #Add ability to continously add etc numbers
answer = QMessageBox()
answerMessage = str(firstNumber) + ' ' + calcType + ' ' + str(secondNumber) + ' = ' + str(output)
answer.setText(answerMessage)
answer.exec()
isFirstNumber = True #Add clear button. May also clash with formatting (int + str). Could resolve by converting back to string after calcs
firstNumber = '0'
secondNumber = '0'
else:
alert = QMessageBox()
alert.setText('You haven''t chosen an operation and/or a second number')
alert.exec()
#Main Routine
firstNumber = '0' #Could pass and return these rather than using as global var
secondNumber = '0'
isFirstNumber = True
app = QApplication([])
window = QWidget()
layout = QGridLayout()
button1 = QPushButton('1')
button2 = QPushButton('2')
button3 = QPushButton('3')
buttonPlus = QPushButton('+')
button4 = QPushButton('4')
button5 = QPushButton('5')
button6 = QPushButton('6')
buttonMinus = QPushButton('-')
button7 = QPushButton('7')
button8 = QPushButton('8')
button9 = QPushButton('9')
buttonMult = QPushButton('*')
button0 = QPushButton('0')
buttonDiv = QPushButton('/')
buttonEqual = QPushButton('=')
displayLabel = QLabel("Test")
layout.addWidget(displayLabel, 3, 3)
layout.addWidget(button1, 0, 0)
layout.addWidget(button2, 0, 1)
layout.addWidget(button3, 0, 2)
layout.addWidget(buttonPlus, 0, 3)
layout.addWidget(button4, 1, 0)
layout.addWidget(button5, 1, 1)
layout.addWidget(button6, 1, 2)
layout.addWidget(buttonMinus, 1, 3)
layout.addWidget(button7, 2, 0)
layout.addWidget(button8, 2, 1)
layout.addWidget(button9, 2, 2)
layout.addWidget(buttonMult, 2, 3)
layout.addWidget(button0, 3, 0)
layout.addWidget(buttonDiv, 3, 1)
layout.addWidget(buttonEqual, 3, 2)
button1.clicked.connect(button1Pressed)
button2.clicked.connect(button2Pressed)
button3.clicked.connect(button3Pressed)
button4.clicked.connect(button4Pressed)
button5.clicked.connect(button5Pressed)
button6.clicked.connect(button6Pressed)
button7.clicked.connect(button7Pressed)
button8.clicked.connect(button8Pressed)
button9.clicked.connect(button9Pressed)
button0.clicked.connect(button0Pressed)
buttonPlus.clicked.connect(buttonPlusPressed)
buttonMinus.clicked.connect(buttonMinusPressed)
buttonMult.clicked.connect(buttonMultPressed)
buttonDiv.clicked.connect(buttonDivPressed)
buttonEqual.clicked.connect(buttonEqualPressed)
window.setLayout(layout)
window.show()
#label = QLabel('Hello World!')
#label.show()
app.exec_()