如何使用PyQt中的按钮退出while循环中的程序

时间:2014-04-14 09:52:33

标签: python pyqt adc

我有以下代码,点击'开始' 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读取,点击按下“开始”键后按钮开始。 as:self.pushButton.clicked.connect( self.Start)

我还有另一个名为“停止”的pushButton_2。点击这个,上面的过程应该停止。请建议,所以我能够做到。

2 个答案:

答案 0 :(得分:5)

在您关于此主题的其他问题中,除what I suggested之外无需执行任何操作:只需使用processEvents即可。只要你能够频繁地调用它(但不经常太多),它应该完全按照你想要的那样做。使用您的第二个示例,以下对我来说很好:

  def Start(self):
    if not self.started:
        self.started = True
        self.StartLoop()

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

  def StartLoop(self):
    DEBUG = 1
    while self.started:
        print "LED on "
        time.sleep(0.05)
        print "LED off "
        time.sleep(0.085)
        QtGui.qApp.processEvents()

答案 1 :(得分:-1)

  1. 此问题很有用:tkinter loop and serial write可以通过两次更改复制它:master.update变为QtGui.qApp.processEventsmaster.after变为QTimer.singleShot。< / p>

  2. 以下是如何使用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的工作示例:

    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()停止循环。