How to run Qthread one after another?
For the following code:
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('Run')
self.button.clicked.connect(self.my_method)
layout = QtWidgets.QGridLayout(self._main)
layout.addWidget(self.button)
layout.addWidget(self.button)
def my_method(self):
for i in range(0,3):
self.n = 5
self.loadthread = MyThread(self.n, self)
self.loadthread.start()
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)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
screen = MyWindow()
screen.show()
sys.exit(app.exec_())
I am getting the output as:
0
1
0
2
3
1
2
3
4
4
0
1
2
3
4
The threads are running concurrently. I want it to run one after another.
Desired output:
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
How can I run the threads only after one is finished?
I tried using thread.wait(). It works. But the UI freezes during that.