1、总代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5 import QtCore, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(320, 240)
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(20, 40, 291, 181))
self.textEdit.setObjectName("textEdit")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(210, 20, 56, 17))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "开始显示"))
class MyWindow(QWidget, Ui_Form):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.show_text)
def show_text(self):
for i in range(1,50):
text = '第' + str(i) + '次输入内容'
self.textEdit.append(text)
if __name__ == "__main__":
# 适配2k等高分辨率屏幕
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
myWin = MyWindow()
myWin.show()
sys.exit(app.exec_())
2、更多功能
# 获取文本框中的内容
self.textEdit.text()
#设置内容
self.textEdit.setText()
# 清空文本框中的内容就可以通过设置内容为空就行了
self.textEdit.setText("")