Guest User

Untitled

a guest
Aug 8th, 2013
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. import time, sys
  2. from PyQt4.QtCore  import *
  3. from PyQt4.QtGui import *
  4.  
  5. class SimulRunner(QObject):
  6.     'Object managing the simulation'
  7.  
  8.     stepIncreased = pyqtSignal(int, name = 'stepIncreased')
  9.     def __init__(self):
  10.         super(SimulRunner, self).__init__()
  11.         self._step = 0
  12.         self._isRunning = True
  13.         self._maxSteps = 20
  14.  
  15.     def longRunning(self):
  16.  
  17.         # reset
  18.         if not self._isRunning:
  19.             self._isRunning = True
  20.             self._step = 0
  21.  
  22.         while self._step  < self._maxSteps  and self._isRunning == True:
  23.             self._step += 1
  24.             self.stepIncreased.emit(self._step)
  25.             time.sleep(0.1)
  26.  
  27.         print('finished...')
  28.  
  29.     def stop(self):
  30.         self._isRunning = False
  31.  
  32. class SimulationUi(QDialog):
  33.     'PyQt interface'
  34.  
  35.     def __init__(self):
  36.         super(SimulationUi, self).__init__()
  37.  
  38.         self.goButton = QPushButton('Go')
  39.         self.stopButton = QPushButton('Stop')
  40.         self.currentStep = QSpinBox()
  41.  
  42.         self.layout = QHBoxLayout()
  43.         self.layout.addWidget(self.goButton)
  44.         self.layout.addWidget(self.stopButton)
  45.         self.layout.addWidget(self.currentStep)
  46.         self.setLayout(self.layout)
  47.  
  48.         self.simulThread = QThread()
  49.         self.simulThread.start()
  50.  
  51.         self.simulRunner = SimulRunner()
  52.         self.simulRunner.moveToThread(self.simulThread)
  53.         self.simulRunner.stepIncreased.connect(self.currentStep.setValue)
  54.  
  55.         # call stop on simulRunner from this (main) thread on click
  56.         self.stopButton.clicked.connect(lambda: self.simulRunner.stop())
  57.         self.goButton.clicked.connect(self.simulRunner.longRunning)
  58.  
  59.  
  60. if __name__ == '__main__':
  61.     app = QApplication(sys.argv)
  62.     simul = SimulationUi()
  63.     simul.show()
  64.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment