Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt5 QThread问题_Python_Pyqt_Pyqt5_Python Multithreading - Fatal编程技术网

Python PyQt5 QThread问题

Python PyQt5 QThread问题,python,pyqt,pyqt5,python-multithreading,Python,Pyqt,Pyqt5,Python Multithreading,我正在尝试获得QT5线程的基础知识。这是我的第一次尝试,结合了各种来源: 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.p

我正在尝试获得QT5线程的基础知识。这是我的第一次尝试,结合了各种来源:

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)
导入系统 从时间上导入睡眠 从PyQt5.QtWidgets导入QApplication、QWidget、QPushButton、QGridLayout 从PyQt5.QtCore导入QThread,QObject ''' 回溯(最近一次呼叫最后一次): 文件“threads.py”,第68行,在 主(系统argv) 文件“threads.py”,第63行,在main中 window=window() 文件“threads.py”,第15行,在_init中__ self.initUi() initUi中第28行的文件“threads.py” self.worker.moveToThread(self.thread) AttributeError:“非类型”对象没有属性“moveToThread” 按任意键继续。 ''' 类窗口(QWidget): 定义初始化(自): super()。\uuuu init\uuuuu() self.initUi() self.low=0 自高=100 self.show() def initUi(self): self.thread=QThread() self.worker=worker(self) self.worker.moveToThread(self.thread) self.thread.start() self.button=QPushButton( '启动长时间运行的任务') self.layout=QGridLayout() self.layout.addWidget(self.button,0,0) self.setLayout(self.layout) def工作者(QObject): 定义初始化(自身,父级): 超级(工作者,自我)。\uuuu初始\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu 你工作吗 def do_工作(自我): 对于范围内的(20): 打印('正在运行…') 睡眠(2) def主(args): app=QApplication(args) window=window() sys.exit(app.exec_()) 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': 主(系统argv) 我已经在代码片段中包含了我得到的错误。
从在线文章中我了解到,在PyQt5中,我不应该将QThread子类化。

您有两个问题,第一个问题是worker必须是一个类才能进行更改:

def Worker(QObject):
do_work()

另一个问题是,您必须通过实例(即self)调用do_work,因为它会发生变化:

def Worker(QObject):
do_work()
致:

在以下部分中,我将展示一个完整的示例:

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)

天哪,这么愚蠢的错误,真不敢相信。谢谢