# 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,
about String How to create objects JavaScript Hundred refining into Immortals 1.15 Legendary Is MCU embedded , It's a cliche Resume the 13th session python Blue Bridge Cup html+css+js Make a simple website home page java Connect to the database to realize basic addition, deletion, modification and query VHDL——JK trigger Java of JDBC programming 3 4j It's not legal python expression _3+4j It's not legal Python expression .【linux】shell: ordinary shell Script exercise