我有以下代码,将在单击 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 读取,它将在单击名为“Start”的按钮后启动,如下所示: self.pushButton.clicked.connect( self.Start)

我还有另一个 pushButton_2 称为“停止”,通过单击此按钮,上述过程应该停止。请提出建议,以便我能够做到这一点。

有帮助吗?

解决方案

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

  2. 这是如何执行您所要求的操作的草图 吉洛普:

    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 的工作示例 吉洛普:

    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) 使循环继续。

    您还可以使用以下命令停止循环 stopLoop() guiLoop 的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top