# time.sleep(5) # time.sleep()是一个阻塞任务,不允许Qt事件循环运行,从而阻止信号正常工作和GUI更新,运行时会等5s后打印所有信息。 解决方案是使用QTimer和QEventLoop替换该GUI睡眠。
self.timer = QTimer(self) #初始化一个定时器
self.timer.timeout.connect(self.fun) #计时结束调用operate()方法
self.timer.start(1000) #设置计时间隔并启动
完整代码如下:
# -*- coding: utf-8 -*-
# @Time : 2019/11/17 20:08
# @Author : dailinqing
# @Email : dailinqing@126.com
# @File : print_to_ui.py
# @Software: PyCharm
import sys
import time
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QTextEdit
from PyQt5 import QtCore, QtGui
from PyQt5.QtCore import QThread,QTimer
class Stream(QtCore.QObject):
"""Redirects console output to text widget."""
newText = QtCore.pyqtSignal(str)
def write(self, text):
self.newText.emit(