如何向PyQt5 GUI添加线程? [英] How to can I add threading to PyQt5 GUI?

查看:367
本文介绍了如何向PyQt5 GUI添加线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经使用QT Designer创建了一个GUI.它工作得很好,但是在更复杂的调用中,它不会更新主窗口并锁定.我想在主窗口中通过不断变化的后端信息更新textEdit的同时运行CustomComplexFunction(),我希望它每2秒运行一次.以下代码似乎正确,并且运行无误,但不会更新textEdit.请注意,我正在导入从QT Designer设计的带按钮和textEdit的.ui文件,没有它,代码将无法运行.

So I have created a GUI using QT Designer. It works pretty well, but on more complex calls it doesn't update the main window and locks up. I want to run my CustomComplexFunction() while updating a textEdit in the main window from constantly changing backend information, and I wanted it to run every 2 seconds. The following code seems right and runs without errors, but doesn't update the textEdit. Please note i'm importing a .ui file designed from QT Designer with a pushButton and textEdit and the code won't run without it.

Main.py

import sys
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout, QMainWindow
from PyQt5.QtCore import QCoreApplication, QObject, QRunnable, QThread, QThreadPool, pyqtSignal, pyqtSlot
from PyQt5 import uic, QtGui

class Worker(QObject):
    newParams = pyqtSignal()

    @pyqtSlot()
    def paramUp(self):
        x=1
        for x in range(100):
            time.sleep(2)
            self.newParams.emit()
            print ("sent new params")
            x +=1

Ui_somewindow, _ = uic.loadUiType("mainwindow.ui") #the path to UI

class SomeWindow(QMainWindow, Ui_somewindow, QDialog):

    def __init__(self):

        QMainWindow.__init__(self)
        Ui_somewindow.__init__(self)
        self.setupUi(self)

        # Start custom functions
        self.params = {}
        self.pushButton.clicked.connect(self.complex) #NumEvent

    def complex(self):
        self.work = Worker() 
        self.thread = QThread()

        self.work.newParams.connect(self.changeTextEdit)
        self.work.moveToThread(self.thread)
        self.thread.start()

        self.CustomComplexFunction()

    def CustomComplexFunction(self):
        self.params['City'] = 'Test'

    def changeTextEdit(self):

        try: 
            City = self.params['City']
            self.textEdit_14.setPlainText(City) 
        except KeyError:
            City = None
if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = SomeWindow()
    window.show()
    sys.exit(app.exec_())

您可以在信号和插槽中查看官方文档 SO帖子也很有帮助,但看来我正确地构建了它.根据文档,发射器不在乎是否使用信号.这可能就是为什么代码没有错误但也不起作用的原因.

You can see the official docs for Signals and Slots here and this SO post was also very helpful, but it seems like I built it correctly. According to the docs, the emitter doesn't care if the signal is used. This might be why the code doesn't have errors but doesn't work either.

有关如何使其工作的任何想法?还是至少可以通过某种方式测试发射器和信号?

Any ideas on how to make it work? Or atleast some way to test the emitter and signals??

推荐答案

您忘记了将线程连接到worker对象.

You have forgot to connect the thread to the worker object.

self.work = Worker()
self.thread = QThread()
self.thread.started.connect(self.worker.work) # <--new line, make sure work starts.
self.thread.start()

该应用程序祝您好运:-)

Good luck with the application :-)

这篇关于如何向PyQt5 GUI添加线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆