当前位置: 首页 > 面试题库 >

pyqt5 qthread +信号不起作用+ GUI冻结

卜泓
2023-03-14
问题内容

我正在尝试使用imaplib做一个邮箱检查器,它在没有gui的python,队列和多线程环境下可以正常工作。

但是,当我尝试放置一个gui时,我所做的每个功能都将gui冻结直到完成。

我尝试了各种文档中的许多东西(添加qthread,信号,游标等),而教程对我没有用。

有人可以帮助我了解如何在运行函数时设置文本或将文本附加到QtextEdit吗,因为它仅在完成后才起作用。

这是我的代码:

class Checker(QtCore.QThread):
    signal = QtCore.pyqtSignal(object)

    def __init__(self, lignesmailtocheck):
        QtCore.QThread.__init__(self)
        self.lignesmailtocheck = lignesmailtocheck

    def run(self):
            lignemailtocheck = self.lignesmailtocheck.strip()                        
            maillo, passo = lignemailtocheck.split(":",1)
            debmail, finmail = maillo.split("@",1)
            setimap =["oultook.com:imap-mail.outlook.com", "gmail.com:imap.gmail.com"]
            for lignesimaptocheck in sorted(setimap):
                    ligneimaptocheck = lignesimaptocheck.strip()
                    fai, imap = ligneimaptocheck.split(":",1)                                
                    if finmail == fai:
                            passo0 = passo.rstrip()
                            try :
                                    mail = imaplib.IMAP4_SSL(imap)
                                    mail.login(maillo, passo)
                                    mailboxok = open("MailBoxOk.txt", "a+", encoding='utf-8', errors='ignore')
                                    mailboxok.write(maillo+":"+passo+"\n")
                                    mailboxok.close()
                                    totaly = maillo+":"+passo0+":"+imap                                
                                    print(maillo+":"+passo+"\n")

                                    self.send_text.emit(totaly)
                                    time.sleep(1)
                            except imaplib.IMAP4.error:                          
                                           print ("LOGIN FAILED!!! ")
class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)

        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(150, 210, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.gogogo)

        self.openliste = QtWidgets.QToolButton(Form)
        self.openliste.setGeometry(QtCore.QRect(40, 110, 71, 21))
        self.openliste.setObjectName("openliste")

        self.textEdit = QtWidgets.QTextEdit(Form)
        self.textEdit.setGeometry(QtCore.QRect(170, 50, 201, 121))
        self.textEdit.setObjectName("textEdit")

        self.progressBar = QtWidgets.QProgressBar(Form)
        self.progressBar.setGeometry(QtCore.QRect(10, 260, 381, 23))
        self.progressBar.setValue(0)
        self.progressBar.setObjectName("progressBar")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))
        self.openliste.setText(_translate("Form", "..."))

    def gogogo(self):

        mailtocheck = open('File/toCheck.txt', 'r', encoding='utf-8', errors='ignore').readlines()        
        setmailtocheck = set(mailtocheck)
        for lignesmailtocheck in sorted(setmailtocheck):
            checker = Checker(lignesmailtocheck)

            thread = QThread()
            checker.moveToThread(thread)
            # connections after move so cross-thread:
            thread.started.connect(checker.run)
            checker.signal.connect(self.checkedok)
            thread.start()

    def checkedok(self, data):
        print(data)
        self.textEdit.append(data)
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

问题答案:

由于QThread与您的情况类似,在PyQt中经常存在有关使用的问题,因此以下示例显示了如何在PyQt中正确使用线程。我希望它可以用作类似问题的解答,因此我比平常花了更多的时间准备它。

该示例创建了多个工作对象,这些工作对象在非主线程中执行,并通过Qt的异步信号与主(即GUI)线程进行通信。

import time
import sys

from PyQt5.QtCore import QObject, QThread, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QApplication, QPushButton, QTextEdit, QVBoxLayout, QWidget


def trap_exc_during_debug(*args):
    # when app raises uncaught exception, print info
    print(args)


# install exception hook: without this, uncaught exception would cause application to exit
sys.excepthook = trap_exc_during_debug


class Worker(QObject):
    """
    Must derive from QObject in order to emit signals, connect slots to other signals, and operate in a QThread.
    """

    sig_step = pyqtSignal(int, str)  # worker id, step description: emitted every step through work() loop
    sig_done = pyqtSignal(int)  # worker id: emitted at end of work()
    sig_msg = pyqtSignal(str)  # message to be shown to user

    def __init__(self, id: int):
        super().__init__()
        self.__id = id
        self.__abort = False

    @pyqtSlot()
    def work(self):
        """
        Pretend this worker method does work that takes a long time. During this time, the thread's
        event loop is blocked, except if the application's processEvents() is called: this gives every
        thread (incl. main) a chance to process events, which in this sample means processing signals
        received from GUI (such as abort).
        """
        thread_name = QThread.currentThread().objectName()
        thread_id = int(QThread.currentThreadId())  # cast to int() is necessary
        self.sig_msg.emit('Running worker #{} from thread "{}" (#{})'.format(self.__id, thread_name, thread_id))

        for step in range(100):
            time.sleep(0.1)
            self.sig_step.emit(self.__id, 'step ' + str(step))

            # check if we need to abort the loop; need to process events to receive signals;
            app.processEvents()  # this could cause change to self.__abort
            if self.__abort:
                # note that "step" value will not necessarily be same for every thread
                self.sig_msg.emit('Worker #{} aborting work at step {}'.format(self.__id, step))
                break

        self.sig_done.emit(self.__id)

    def abort(self):
        self.sig_msg.emit('Worker #{} notified to abort'.format(self.__id))
        self.__abort = True


class MyWidget(QWidget):
    NUM_THREADS = 5

    # sig_start = pyqtSignal()  # needed only due to PyCharm debugger bug (!)
    sig_abort_workers = pyqtSignal()

    def __init__(self):
        super().__init__()

        self.setWindowTitle("Thread Example")
        form_layout = QVBoxLayout()
        self.setLayout(form_layout)
        self.resize(400, 800)

        self.button_start_threads = QPushButton()
        self.button_start_threads.clicked.connect(self.start_threads)
        self.button_start_threads.setText("Start {} threads".format(self.NUM_THREADS))
        form_layout.addWidget(self.button_start_threads)

        self.button_stop_threads = QPushButton()
        self.button_stop_threads.clicked.connect(self.abort_workers)
        self.button_stop_threads.setText("Stop threads")
        self.button_stop_threads.setDisabled(True)
        form_layout.addWidget(self.button_stop_threads)

        self.log = QTextEdit()
        form_layout.addWidget(self.log)

        self.progress = QTextEdit()
        form_layout.addWidget(self.progress)

        QThread.currentThread().setObjectName('main')  # threads can be named, useful for log output
        self.__workers_done = None
        self.__threads = None

    def start_threads(self):
        self.log.append('starting {} threads'.format(self.NUM_THREADS))
        self.button_start_threads.setDisabled(True)
        self.button_stop_threads.setEnabled(True)

        self.__workers_done = 0
        self.__threads = []
        for idx in range(self.NUM_THREADS):
            worker = Worker(idx)
            thread = QThread()
            thread.setObjectName('thread_' + str(idx))
            self.__threads.append((thread, worker))  # need to store worker too otherwise will be gc'd
            worker.moveToThread(thread)

            # get progress messages from worker:
            worker.sig_step.connect(self.on_worker_step)
            worker.sig_done.connect(self.on_worker_done)
            worker.sig_msg.connect(self.log.append)

            # control worker:
            self.sig_abort_workers.connect(worker.abort)

            # get read to start worker:
            # self.sig_start.connect(worker.work)  # needed due to PyCharm debugger bug (!); comment out next line
            thread.started.connect(worker.work)
            thread.start()  # this will emit 'started' and start thread's event loop

        # self.sig_start.emit()  # needed due to PyCharm debugger bug (!)

    @pyqtSlot(int, str)
    def on_worker_step(self, worker_id: int, data: str):
        self.log.append('Worker #{}: {}'.format(worker_id, data))
        self.progress.append('{}: {}'.format(worker_id, data))

    @pyqtSlot(int)
    def on_worker_done(self, worker_id):
        self.log.append('worker #{} done'.format(worker_id))
        self.progress.append('-- Worker {} DONE'.format(worker_id))
        self.__workers_done += 1
        if self.__workers_done == self.NUM_THREADS:
            self.log.append('No more workers active')
            self.button_start_threads.setEnabled(True)
            self.button_stop_threads.setDisabled(True)
            # self.__threads = None

    @pyqtSlot()
    def abort_workers(self):
        self.sig_abort_workers.emit()
        self.log.append('Asking each worker to abort')
        for thread, worker in self.__threads:  # note nice unpacking by Python, avoids indexing
            thread.quit()  # this will quit **as soon as thread event loop unblocks**
            thread.wait()  # <- so you need to wait for it to *actually* quit

        # even though threads have exited, there may still be messages on the main thread's
        # queue (messages that threads emitted before the abort):
        self.log.append('All threads exited')


if __name__ == "__main__":
    app = QApplication([])

    form = MyWidget()
    form.show()

    sys.exit(app.exec_())

以下是了解PyQt中多线程编程所必需的主要概念:

  • Qt线程有自己的事件循环(特定于每个线程)。主线程(也称为GUI线程)也是一个QThread,其事件循环由该线程管理。
  • 线程之间的信号通过接收线程的事件循环(异步)进行传输。因此,GUI或任何线程的响应能力=处理事件的能力。例如,如果线程在函数循环中处于繁忙状态,则它无法处理事件,因此在函数返回之前,它不会响应来自GUI的信号。
  • 如果线程中的辅助对象(方法)可能必须根据来自GUI的信号更改其操作过程(例如,中断循环或等待),则它必须调用processEvents()QApplication实例。这将允许QThread处理事件,从而响应来自GUI的异步信号调用插槽。请注意,QApplication.instance().processEvents()似乎processEvents()在每个线程上调用,如果不希望这样做,那么这QThread.currentThread().processEvents()是一个有效的选择。
  • 调用QThread.quit()不会立即退出其事件循环:它必须等待当前正在执行的插槽(如果有)返回。因此,一旦告诉线程退出,您必须在其上等待。所以平时中止工作线程包括信令,但它(通过自定义信号),停止不管它是这样做的:这需要一个GUI对象上的自定义信号,该信号的一个工人插槽的连接,和工人的工作方法必须调用线程的processEvents()到在工作时允许发射的信号到达插槽。


 类似资料:
  • 我是的新手,所以我试图为一个程序(我试图制作一个编译器)制作一个可执行文件,该程序使用一个setup.py脚本的模块: 我使用的是python 3.5,当我打开生成的向我抛出以下错误: 我怎样才能纠正这个错误?

  • 我试图将消息设置为示例留档。我所有的服务和类都装饰有组件或服务注释,但我仍然得到以下例外: org.springframework.messaging.MessageDeliveryException:Dispatcher没有频道unknown.channel.name的订阅者 应用程序设置增加了:cloud:stream:kafka:binder:brokers:localhost zk nod

  • 问题内容: 我知道此问题是由主线程上的sleep或wait调用引起的,有关如何解决此问题的答案是将方法放入单独的线程中,然后使该线程进入休眠状态。但是代码是一团糟,真的没有时间将其整理出来并分成单独的线程,并且想知道是否还有其他方法可以做到这一点?即使这不是使用GUI的最干净或最常见的做法。我只需要从方法中暂停一秒钟。 问题答案: 不创建单独的线程就无法做到这一点。用Java创建线程很容易。唯一要

  • 我们已经实施贝宝支付到我的网站。 我们正在用沙箱测试模式进行检查。付款通过登录与贝宝帐户工作罚款,但没有登录贝宝帐户使用卡选项付款不工作。我正在尝试测试卡的详细信息,它会显示这样的错误信息:“您输入的卡不能用于此付款。请输入其他的借记卡或信用卡号码。” 我有用户所有由贝宝提供的卡号仍有错误 https://developer.paypal.com/webapps/developer/docs/cl

  • 我想使用与GUI相结合。我是的新手,对它的理解不是很详细。这里的示例在单击第一个按钮时启动10个任务。这个任务只是在几秒钟内用来模拟工作。 示例代码在Python下运行良好。但问题是GUI被冻结了。当我按下第一个按钮并启动10个异步任务时,我不能在GUI中按下第二个按钮,直到所有任务都完成。图形用户界面永远不应该冻结——这是我的目标。 ...由于此错误,我无法再次运行该任务。 为什么多线程是一个可

  • 问题内容: 我正在尝试并行化脚本,但是由于未知的原因,内核只是冻结而没有引发任何错误。 最小的工作示例: 有趣的是,如果我在另一个文件中定义函数然后将其导入,则一切都可以正常工作。如何使它工作而无需另一个文件? 我使用spyder(anaconda),如果从Windows命令行运行代码,则结果相同。 问题答案: 发生这种情况是因为在子进程导入时,您没有保护代码的“过程”部分免于重新执行。 它们需要