Ahmed_Ab96

[PyQt5] How to pause , resume & stop QThread.

Aug 23rd, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. from PyQt5.QtWidgets import *
  2. from PyQt5.QtGui import *
  3. from PyQt5.QtCore import *
  4. import sys
  5. import time
  6.  
  7. class main(QWidget):
  8.     finieshed=pyqtSignal()
  9.     Signal=pyqtSignal()
  10.     def __init__(self):
  11.         super(main,self).__init__()
  12.         vb=QVBoxLayout()
  13.         self.StartAndPauseBtn=QPushButton('Start')
  14.         self.StopBtn=QPushButton('Stop')
  15.  
  16.  
  17.         vb.addWidget(self.StartAndPauseBtn)
  18.         vb.addWidget(self.StopBtn)
  19.         self.setLayout(vb)
  20.  
  21.         self.worker=worker()
  22.         self.mythread=QThread()
  23.         self.worker.moveToThread(self.mythread)
  24.         self.mythread.start()
  25.  
  26.         self.StartAndPauseBtn.clicked.connect(self.on_startBtn)
  27.  
  28.         self.StopBtn.clicked.connect(self.on_stopBtn)
  29.         self.StopBtn.setDisabled(1) #stop button disabled change to 1 or True to be enable
  30.  
  31.     def on_startBtn(self):
  32.         try:
  33.             if self.StartAndPauseBtn.text()=="Start":
  34.                 self.Signal.connect(self.worker.start)
  35.                 self.Signal.emit()
  36.  
  37.                 self.worker.signal.connect(self.worker.start)
  38.                 self.worker.Stop=False   # look will work and not stopped
  39.                 self.worker.Pause=True # loop will work and not paused
  40.                 self.StartAndPauseBtn.setText('Pause')
  41.                 self.StopBtn.setDisabled(0) #stop buttn will be enable
  42.             else:
  43.                 self.StartAndPauseBtn.setText('Start')
  44.                 self.worker.Paus_Thread()
  45.                 self.worker.Stop=False # it will make thee loop Pause
  46.  
  47.         except Exception as e:
  48.             print(str(e))
  49.  
  50.     def on_stopBtn(self):
  51.         self.worker.stop_thread() #loop will be stopped
  52.         self.StartAndPauseBtn.setText('Start')
  53.         print('Stopped')
  54.         self.StopBtn.setDisabled(1) #stop button will be disable
  55.  
  56. class worker(QObject):
  57.     signal=pyqtSignal()
  58.     def __init__(self):
  59.         super(worker,self).__init__()
  60.  
  61.         self.Pause = True
  62.         self.Stop=False
  63.  
  64.     def start(self):
  65.         for i in range(15):
  66.             if not self.Pause:
  67.                 print('paused')
  68.                 while not self.Pause : #if this be True it will sleep or pause the loop
  69.                     time.sleep(1)
  70.             if not self.Stop:  # if this be true  it will stop the loop
  71.                 print(i)
  72.                 time.sleep(1)
  73.  
  74.  
  75.     def Paus_Thread(self):
  76.         self.Pause=False
  77.  
  78.     def stop_thread(self):
  79.         self.Stop=True
  80.         self.Pause=True
  81.  
  82. if __name__=='__main__':
  83.     app=QApplication(sys.argv)
  84.     Main=main()
  85.     Main.show()
  86.     sys.exit(app.exec_())
Add Comment
Please, Sign In to add comment