When calling a thread class in PyQt5, the application crashes with error: QThread: Destroyed while thread is still running if I don't add
def __del__(self):
self.wait()
After adding the above statement to the thread class, the application runs but stalls until the thread has finished executing (basically making the thread useless). I'm running macOS Catalina 10.15.6, Intel i9, Python 3.7.4. Any way to resolve this? Here is the code (the ui window only has one button in it):
from PyQt5.QtWidgets import QMainWindow
from PyQt5 import QtWidgets, uic
from PyQt5.QtCore import QThread, pyqtSignal
import sys, time
class Main(QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
uic.loadUi('main.ui', self)
self.btn1.clicked.connect(self.run_worker)
def run_worker(self):
worker = Worker_thread()
worker.start()
class Worker_thread(QThread):
def __init__(self):
QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
time.sleep(10)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
win = Main()
win.show()
sys.exit(app.exec_())