python - How to quit the program in while loop using push-button in PyQt -


i have following code start after clicking 'start' button in pyqt:

def start(self):   import time   import os   import rpi.gpio gpio   import datetime    gpio.setmode(gpio.bcm)   debug = 1    os.system('clear')    # spi port on gpio   spiclk = 18   spimiso = 23   spics = 25    # set 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()         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)" 

the above program adc reading , start after clicking pushbutton called 'start' : self.pushbutton.clicked.connect( self.start)

and have pushbutton_2 called 'stop' , clicking above process should stop.please suggest, can able that.

  1. this question useful: tkinter loop , serial write copied on 2 changes: master.update becomes qtgui.qapp.processevents , master.after becomes qtimer.singleshot.

  2. here sketch of how ask guiloop:

    from guiloop import guiloop, stoploop # ... means fill in code class ...:      started = false      def start(self):         if not self.started:             # can use threads here, see first link             self.started = self.startloop()      def stop(self):         if self.started:             stoploop(self.started)             self.started = false      @guiloop     def startloop(self):         # start function         # ...         while true:             # ...             yield 0.085 # time.sleep(0.085) equivalent             # ... 

    since not know code like, here working example using pyqt4 , guiloop:

    from pyqt4 import qtgui import sys  guiloop import guiloop # https://gist.github.com/niccokunzmann/8673951  @guiloop def led_blink(argument):     while 1:         print("led on " + argument)         yield 0.5 # time 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 uses qtimer.singleshot(time, function) make loop continue.

    you can stop loop stoploop() of guiloop.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -