QThread
не имеет метода join()
, поэтому ваше приложение должно неожиданно завершить работу и указать следующее сообщение об ошибке.
QLayout: Attempting to add QLayout "" to MyWindow "", which already has a layout
QWidget::setLayout: Attempting to set QLayout "" on MyWindow "", which already has a layout
0
1
2
3
Traceback (most recent call last):
File "main.py", line 24, in MyMethod
self.loadthread.join() # Will wait for the thread until it finishes its task
AttributeError: 'MyThread' object has no attribute 'join'
4
cycle finished
Aborted (core dumped)
Если вы хотите выполнить какое-либо задание после его завершенияпоток, который вы должны использовать finished
сигнал QThread
:
import sys
from PyQt5 import QtCore, QtWidgets
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('MyWindow')
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
self.button = QtWidgets.QPushButton('Do it')
self.button.clicked.connect(self.my_method)
layout = QtWidgets.QGridLayout(self._main)
layout.addWidget(self.button)
layout.addWidget(self.button)
@QtCore.pyqtSlot()
def my_method(self):
self.n = 5
self.loadthread = MyThread(self.n, self)
self.loadthread.finished.connect(self.on_finished)
self.loadthread.start()
@QtCore.pyqtSlot()
def on_finished(self):
print('thread finished')
class MyThread(QtCore.QThread):
def __init__(self, n, parent=None):
QtCore.QThread.__init__(self, parent)
self.n = n
def run(self):
for i in range(self.n):
print(i)
print('cycle finished')
if __name__ == '__main__':
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
mainGui = MyWindow()
mainGui.show()
app.aboutToQuit.connect(app.deleteLater)
sys.exit(app.exec_())