如何使用PyQt中的按钮在while循环中退出程序 [英] How to quit the program in while loop using push-button in PyQt

查看:402
本文介绍了如何使用PyQt中的按钮在while循环中退出程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,这些代码将在PyQt中单击开始"按钮后启动:

I have the following code which will start after clicking the 'Start' button in PyQt:

def Start(self):
  import time
  import os
  import RPi.GPIO as GPIO
  import datetime

  GPIO.setmode(GPIO.BCM)
  DEBUG = 1

  os.system('clear')

  # SPI port on GPIO
  SPICLK = 18
  SPIMISO = 23
  SPICS = 25

  # set up the SPI interface pins
  GPIO.setup(SPIMISO, GPIO.IN)
  GPIO.setup(SPICLK, GPIO.OUT)
  GPIO.setup(SPICS, GPIO.OUT)

  GPIO.output(SPICS, True)
  GPIO.output(SPICS, False) # bring CS low
  while True:
        adcout = 0             
        read_adc = 0
        #s=time.clock()
        for i in range(25):
            GPIO.output(SPICLK, True)
            GPIO.output(SPICLK, False)
            adcout <<= 1
            if (GPIO.input(SPIMISO)==1):
                adcout |= 0x1
        time.sleep(0.085)   
        if (GPIO.input(SPIMISO)==0):
            read_adc = adcout
            millivolts = read_adc * ( 2500.0 /(pow(2,22)))
            read_adc = "%d" % read_adc
            millivolts = "%d" % millivolts

        if DEBUG:
            print millivolts, "mV (ADC)"

以上程序用于ADC读取,它将在单击名为开始"的按钮后以以下方式启动:self.pushButton.clicked.connect( self.Start)

The above program is for ADC reading and it will start after clicking the pushbutton called 'Start' as : self.pushButton.clicked.connect( self.Start)

我还有一个名为'Stop'的pushButton_2,单击此按钮,以上过程应停止.请提出建议,以便我能够做到这一点.

And I have another pushButton_2 called 'Stop' and by clicking this the above process should stop.Please suggest, so I can able to do that.

推荐答案

  1. 这个问题很有用: tkinter循环和串行写入复制有两个更改:master.update变为QtGui.qApp.processEventsmaster.after变为QTimer.singleShot.

  1. This question is useful: tkinter loop and serial write It could be copied over with two changes: master.update becomes QtGui.qApp.processEvents and master.after becomes QTimer.singleShot.

这是使用 guiLoop 的要求的草图. :

Here is a sketch of how to do what you ask for with guiLoop:

from guiLoop import guiLoop, stopLoop
# ... means fill in your code
class ...: 
    started = False

    def Start(self):
        if not self.started:
            # you can also use threads here, see the first link
            self.started = self.StartLoop()

    def Stop(self):
        if self.started:
            stopLoop(self.started)
            self.started = False

    @guiLoop
    def StartLoop(self):
        # This is your Start function
        # ...
        while True:
            # ...
            yield 0.085 # time.sleep(0.085) equivalent
            # ...

由于我不知道您的代码是什么样子,因此以下是使用PyQT4和 guiLoop :

Since I do not know what your code look like, here is a working example using PyQT4 and guiLoop:

from PyQt4 import QtGui
import sys

from guiLoop import guiLoop # https://gist.github.com/niccokunzmann/8673951

@guiLoop
def led_blink(argument):
    while 1:
        print("LED on " + argument)
        yield 0.5 # time to wait
        print("LED off " + argument)
        yield 0.5

app = QtGui.QApplication(sys.argv)

w = QtGui.QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()

led_blink(w, 'shiny!')

sys.exit(app.exec_())

guiLoop使用QTimer.singleShot(time, function)使循环继续.

您还可以使用guiLoop的stopLoop()停止循环.

You can also stop the loop with stopLoop() of guiLoop.

这篇关于如何使用PyQt中的按钮在while循环中退出程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆