Save New Duplicate & Edit Just Text Twitter
# importing libraries
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
from PyQt6.QtCore import *
import sys
import time
class Worker(QObject):
finished = pyqtSignal()
_stop = False
def run(self):
i=0
print('Started')
for i in range(5):
if self._stop:
print('Breaking')
break
time.sleep(1)
i+=1
self.finished.emit()
print('Done')
def stop(self):
self._stop = True
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
try:
self.worker.stop()
self.thread.wait()
except (RuntimeError,AttributeError):
pass
print('Here')
self.createThread()
self.thread.started.connect(self.worker.run)
self.worker.quit = False
self.thread.finished.connect(lambda:print('Stopped'))
self.thread.start()
self.btn = QPushButton(self)
self.btn.move(40, 80)
self.btn.setText('Stop')
self.btn.clicked.connect(self.initUI)
self.setWindowTitle("Python")
self.show()
def createThread(self):
self.thread = QThread()
self.worker = Worker()
self.worker.moveToThread(self.thread)
self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
if __name__ == '__main__':
App = QApplication(sys.argv)
window = Example()
sys.exit(App.exec())
I wrote an MVCE version of a problem I have been having.
The initUI function creates a window with a button in it. It also creates a thread that waits for 5 loops with a 1 second sleep each loop. When I click the button, it calls initUI again. This time, if the thread is still running, it calls the worker.stop function to stop the thread in between. When I do this, although the worker.run function finishes execution, the worker.finished signal is not emitted. However, the signal is emitted when the loop finishes on its own without pressing the button (i.e. waiting 5 seconds). Any explanation?