[英]PyQt5 QThread Issue
I am trying to get the basics of QT5 threading. 我试图获得QT5线程的基础知识。 This is my first attempt, combined from various sources:
这是我的第一次尝试,结合了各种来源:
import sys
from time import sleep
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout
from PyQt5.QtCore import QThread, QObject
'''
Traceback (most recent call last):
File "threads.py", line 68, in <module>
main(sys.argv)
File "threads.py", line 63, in main
window = Window()
File "threads.py", line 15, in __init__
self.initUi()
File "threads.py", line 28, in initUi
self.worker.moveToThread(self.thread)
AttributeError: 'NoneType' object has no attribute 'moveToThread'
Press any key to continue . . .
'''
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUi()
self.low = 0
self.high = 100
self.show()
def initUi(self):
self.thread = QThread()
self.worker = Worker(self)
self.worker.moveToThread(self.thread)
self.thread.start()
self.button = QPushButton(
'Start long running task')
self.layout = QGridLayout()
self.layout.addWidget(self.button, 0, 0)
self.setLayout(self.layout)
def Worker(QObject):
def __init__(self, parent):
super(Worker, self).__init__(parent)
do_work()
def do_work(self):
for _ in range(20):
print('running . . .')
sleep(2)
def main(args):
app = QApplication(args)
window = Window()
sys.exit(app.exec_())
if __name__ == '__main__':
main(sys.argv)
I have included the error I get in the code snippet. 我在代码片段中包含了错误。 From online articles i learned that in PyQt5 I shouldn't subclass QThread.
从在线文章我了解到,在PyQt5中我不应该继承QThread。
You have 2 problems, the first is that the worker must be a class for it changes: 你有两个问题,第一个是工人必须是一个改变它的类:
def Worker(QObject):
to 至
class Worker(QObject):
The other problem is that you must call do_work by means of the instance, ie self, for it changes: 另一个问题是你必须通过实例调用do_work,即self,因为它会改变:
do_work()
to: 至:
self.do_work()
In the following part I show a complete example: 在下面的部分中,我展示了一个完整的例子:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout
from PyQt5.QtCore import QThread, QObject
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUi()
self.low = 0
self.high = 100
self.show()
def initUi(self):
self.thread = QThread()
self.worker = Worker()
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.do_work)
self.thread.finished.connect(self.thread.deleteLater)
self.button = QPushButton(
'Start long running task')
self.button.clicked.connect(self.thread.start)
self.layout = QGridLayout()
self.layout.addWidget(self.button, 0, 0)
self.setLayout(self.layout)
class Worker(QObject):
def __init__(self, parent=None):
QObject.__init__(self, parent=parent)
def do_work(self):
for _ in range(20):
print('running . . .')
QThread.sleep(2)
def main(args):
app = QApplication(args)
window = Window()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main(sys.argv)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.