python - 导入中的停止循环
问题描述
我正在使用 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_())
如何在不冻结的情况下导入它?
解决方案
推荐阅读
- angular - 使用角度将多 json 映射到对象
- c# - 如何强制 Visual Studio 使用特定的框架版本
- swiftui - 文本会被 SwiftUI 剪切,但是为什么呢?
- php - 计算图像中任意颜色质心并将其提供给 PHP 的最快方法
- python - 如何迭代 Django 模型字段,并根据条件添加值
- html - 将图像定位在屏幕左侧
- linux - 如何返回默认的 Android Studio 工具栏?
- python - 使用关系运算符元素对 numpy 数组的多个索引进行操作
- javascript - .net core web api post使用fetch出现500错误
- python - 如何在 django 通用视图中本地化日期字段