0

通过终端运行程序时,我们可以通过按“Ctrl+c”停止程序,它将显示消息为“KeyboardInterrupt”。那么,有什么方法可以通过单击 PyQt 中的按钮来完成理智的事情。

4

2 回答 2

1

如果您的程序正在运行一个循环,您可以定期调用processEvents以允许 gui 有时间更新(这应该允许您单击一个按钮来关闭应用程序):

    count = 0
    while True:
         count += 1
         if not count % 50:
             QtGui.qApp.processEvents()
         # do stuff...
于 2014-04-12T16:58:46.263 回答
0

在我的脚本中,我也使用了中断无限循环QtGui.qApp.processEvents(),结果很好。无限循环从串行端口写入和读取数据,用户可以通过按钮 (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
于 2019-01-10T11:52:38.207 回答