首页 > 解决方案 > 导入中的停止循环

问题描述

我正在使用 pyqt5 制作一个自定义控制台/shell 窗口模块,我基本上一切正常,但是当我导入它时,由于我导入的模块中有一个循环,整个脚本都会冻结。这是导入的模块:

import sys
import time

from PyQt5 import QtWidgets, uic
from PyQt5.QtCore import QRunnable, QThreadPool
from PyQt5.QtWidgets import QTextEdit


class SpamWorker(QRunnable):
    def __init__(self, console: QTextEdit):
        super().__init__()
        self.console = console

    def run(self):
        while True:
            time.sleep(0.2)
            self.console.append('SPAM!')


class Printer(QtWidgets.QMainWindow, QRunnable):
    def __init__(self):
        super(Printer, self).__init__()
        uic.loadUi('window.ui', self)
        self.setWindowTitle("Console+")
        self.thread_pool = QThreadPool()

        self.thread_pool.start(SpamWorker(self.Console))


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Printer()
    window.show()
    sys.exit(app.exec_())

如何在不冻结的情况下导入它?

标签: pythonmultithreadingimportpyqt5

解决方案


推荐阅读