说明:此文章主要为了记录在界面文本显示区域,实时显示最新数据,及将滚动条固定在最下方。
代码复制可用。
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class ClassTextAreaWrite(QtWidgets.QWidget):
def __init__(self):
super(ClassTextAreaWrite, self).__init__()
self.init_ui()
self.bind_trigger()
def init_ui(self):
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle("PushButtonClicked")
button = QtWidgets.QPushButton(text="Start", parent=self)
self.button = button
timer = QtCore.QTimer()
self.timer = timer
view_area = QtWidgets.QPlainTextEdit(parent=self)
view_area.setGeometry(10, 60, 280, 100)
self.view_area = view_area
def write(self, text):
""""""
cursor = self.view_area.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(text + "\n")
self.view_area.setTextCursor(cursor)
self.view_area.ensureCursorVisible()
def bind_trigger(self):
# Button clicked trigger
self.button.clicked.connect(lambda: self.timer.start(1))
self.timer.timeout.connect(self.clicked_button)
def clicked_button(self):
self.write("Write Num: {0}".format(self.timer.interval()))
self.timer.setInterval(self.timer.interval() + 1)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
push_button_clicked = ClassTextAreaWrite()
push_button_clicked.show()
sys.exit(app.exec_())
Tips: 在PyQt中使用time.sleep(1)会导致界面阻塞。所以此处使用了QTimer,更多详细内容请见专栏QTimer文章。