PyQt5 多线程 子线程的任务怎么跑到主线程去运行了?

发布于 2022-09-06 04:09:45 字数 1496 浏览 12 评论 0

我使用PyQt5写GUI程序,老是出现“未响应”的情况,将耗时操作放入到子线程去运行也是如此,我写了下面一个小程序来测试,发现这些任务的确是在主线程执行的,各位大神来求解释?我的代码如下:

from PyQt5.QtCore import QThread, QObject, QCoreApplication, qDebug, QTimer


class Worker(QObject):
    def on_timeout(self):
        qDebug('Worker.on_timeout get called from: %s' % hex(int(QThread.currentThreadId())))


if __name__ == '__main__':
    import sys

    app = QCoreApplication(sys.argv)
    qDebug('From main thread: %s' % hex(int(QThread.currentThreadId())))
    t = QThread()
    worker = Worker()
    timer = QTimer()
    timer.timeout.connect(worker.on_timeout)
    timer.start(1000)
    timer.moveToThread(t)
    worker.moveToThread(t)
    t.start()

    app.exec_()

Windows下的输出是:

From main thread: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4
Worker.on_timeout get called from: 0x7f4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2022-09-13 04:09:45

这样写

from PyQt5.QtCore import QThread ,  pyqtSignal,  QDateTime , QObject
from PyQt5.QtWidgets import QApplication,  QDialog,  QLineEdit
import time
import sys

class BackendThread(QObject):
    # 通过类成员对象定义信号
    update_date = pyqtSignal(str)
    
    # 处理业务逻辑
    def run(self):
        while True:
            data = QDateTime.currentDateTime()
            currTime = data.toString("yyyy-MM-dd hh:mm:ss")
            self.update_date.emit( str(currTime) )
            time.sleep(1)

class Window(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.setWindowTitle('PyQt 5界面实时更新例子')
        self.resize(400, 100)
        self.input = QLineEdit(self)
        self.input.resize(400, 100)
        self.initUI()

    def initUI(self):
        # 创建线程
        self.backend = BackendThread()
        # 连接信号
        self.backend.update_date.connect(self.handleDisplay)
        self.thread = QThread()
        self.backend.moveToThread(self.thread)
        # 开始线程
        self.thread.started.connect(self.backend.run)
        self.thread.start()

    # 将当前时间输出到文本框
    def handleDisplay(self, data):
        self.input.setText(data)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Window()
    win.show() 
    sys.exit(app.exec_())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文