# time.sleep(5) #
time.sleep() It's a blocking task , not allow Qt Event loop operation , This prevents the signal from working properly and GUI to update , The runtime will wait 5s Then print all information .
The solution is to use QTimer and QEventLoop Replace the GUI sleep . self.timer = QTimer(self) # Initialize a timer
self.timer.timeout.connect(self.fun) # Timing end call operate() method self.timer.start(1000)
# Set the timer interval and start
 

The complete code is as follows :
# -*- 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(str(text)) class QMyWindow(QMainWindow): """Main application
window.""" def __init__(self): super().__init__() self.initUI() #
Note out this sentence and print it to the console , Convenient debugging sys.stdout = Stream(newText=self.onUpdateText) # Initialize a timer
self.timer = QTimer(self) # The timer timeout signal is combined with the slot function showTime() connect
self.timer.timeout.connect(self.fun) self.num = 0 def fun(self): self.num += 1
print(self.num) def onUpdateText(self, text): """Write console output to text
widget.""" cursor = self.process.textCursor()
cursor.movePosition(QtGui.QTextCursor.End) cursor.insertText(text)
self.process.setTextCursor(cursor) self.process.ensureCursorVisible() def
closeEvent(self, event): """Shuts down application on close.""" # Return stdout
to defaults. sys.stdout = sys.__stdout__ super().closeEvent(event) def
initUI(self): """Creates UI window on launch.""" # Button for generating the
master list. btn = QPushButton('Run', self) btn.move(450, 100) btn.resize(100,
100) btn.clicked.connect(self.OnBtnClicked) # Create the text output widget.
self.process = QTextEdit(self, readOnly=True)
self.process.ensureCursorVisible() self.process.setLineWrapColumnOrWidth(500)
self.process.setLineWrapMode(QTextEdit.FixedPixelWidth)
self.process.setFixedWidth(400) self.process.setFixedHeight(150)
self.process.move(30, 100) # Set window size and title, then show the window.
self.setGeometry(300, 300, 600, 300) self.setWindowTitle('print to ui')
self.show() def OnBtnClicked(self): """Runs the main function."""
print('Running...') self.timer.start(1000) # time.sleep(5) #
time.sleep() It's a blocking task , not allow Qt Event loop operation , This prevents the signal from working properly and GUI to update , The solution is to use QTimer and QEventLoop Replace the GUI sleep .
print('Done.') if __name__ == '__main__': # Run the application. app =
QApplication(sys.argv) # app.aboutToQuit.connect(app.deleteLater) gui =
QMyWindow() print("main") sys.exit(app.exec_())
 

Technology
©2019-2020 Toolsou All rights reserved,
AWS Elastic Compute Cloud (Amazon EC2) Study notes ControllerAdvice Common scenes of PTA-MOOC《Python Programming Zhejiang University 》 Puzzle A Chapter 2 programming problems Dart In Isolatejetson Nano debugging CSI Interface camera , Combined use opencv Turn on the camera Knowledge points of preparing for the Blue Bridge Cup find method Force button 496 The next bigger element IBert/Transformer Calculation of model parameters 2020 year Java industry , General analysis of market environment and position Lambda Expression and functional interface