While running program through the terminal we can stop the program by pressing 'Ctrl+c' and it will show the message as 'KeyboardInterrupt' . So, is there any way to do the sane thing by clicking the push-button in PyQt.
Asked
Active
Viewed 2,139 times
0
-
1Why would you want to raise a `KeyboardInterrupt` error when _clicking_ a button? Or do you mean you just want to quit the program: i.e. `button.clicked.connect(QtGui.qApp.quit)`? – ekhumoro Apr 12 '14 at 13:22
-
But thing is that, I am doing program for ADC and ones the ADC starts reading under the 'while True:' loop then I am not able to stop it. And alternate option is giving interrupt of keyboard. Is there any way to stop the program as my program is running as root. – lkkkk Apr 12 '14 at 16:12
-
Always include information like that in your question, as it makes it _much_ easier to understand. Anyway, I have given an answer below which should solve your issue. – ekhumoro Apr 12 '14 at 17:02
2 Answers
1
If your program is running a loop, you can call processEvents periodically to allow the gui time to update (which should allow you to click a button to close the application):
count = 0
while True:
count += 1
if not count % 50:
QtGui.qApp.processEvents()
# do stuff...

ekhumoro
- 115,249
- 20
- 229
- 336
-
I have posted another question related to above please suggest. Question link is : http://stackoverflow.com/questions/23057031/how-to-quit-the-program-in-while-loop-using-push-button-in-pyqt. I posted this after applying your above given answer but it didn't work,I don't know what is wrong. – lkkkk Apr 14 '14 at 09:58
0
In my script to interrupt an infinite loop I also used QtGui.qApp.processEvents()
and it worked out fine. The infinite loop writes to and reads data from a serial port and the user can interrupt the loop with a push button (1.condition).
def Move_Right(self):
# move the slide right
cmdPack = struct.pack(cmdStruct, Address, Rotate_Right, 0, Motor5, Speed5)
dataByte = bytearray(cmdPack)
checksumInt = sum(dataByte[:]) % 256
msgPack = struct.pack(msgStruct, Address, Rotate_Right, 0, Motor5, Speed5, checksumInt)
ser0.flushOutput() # Clear output buffer
ser0.write(msgPack)
# read the switch status
cmdPack = struct.pack(cmdStruct, Address, Command.GAP, 10, Motor5, 0)
dataByte = bytearray(cmdPack)
checksumInt = sum(dataByte[:]) % 256
msgPack = struct.pack(msgStruct, Address, Command.GAP, 10, Motor5, 0, checksumInt)
ser0.flushOutput() # Clear output buffer
# check the switch status with an infinite write/read loop with two break out conditions
while True:
QtGui.qApp.processEvents() # 1. condition: interrupt with push button
ser0.write(msgPack)
reply = ser0.read(9)
answer = struct.unpack('>BBBBlB', reply)
value = answer[4]
command = answer[3]
if (command == 6) and (value == 1): # 2. condition: interrupt with limit switch
print 'end of line'
Stop_Motor5()
break

parovelb
- 353
- 4
- 19