本文主要是介绍PyQt5入门学习笔记(五)—QTextEdit、QTextBrowser,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
QTextEdit
介绍
QTextEdit类提供了一个用于编辑和显示纯文本和富文本的部件。
方法和属性
**1.toPlainText():获取文本
**2.setText():设置文本
**3.textChanged():再文本每次被改变的时候,发送信号
**4.clear():清空文本
**5.setPlaceholderText():设置占位字符串(只有再文本编辑框么有任何内容的时候才会显示)
制作一个简易的记事本
import sys
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton,QTextEdit,QVBoxLayout,QHBoxLayout,QMessageBox
from PyQt5.QtGui import QIconclass Simple_Notebook(QWidget):def __init__(self):super(Simple_Notebook,self).__init__() #使用super函数可以实现子类使用父类的方法self.setWindowTitle("记事本")self.setWindowIcon(QIcon('NoteBook.png'))self.resize(500,500)self.text = QTextEdit(self)self.text.setText("Have a nice day")self.text.setPlaceholderText("Please add some here")self.text.textChanged.connect(lambda:print("it is changed"))self.button_1 = QPushButton("Save",self)self.button_2 = QPushButton("Clear",self)self.button_1.clicked.connect(lambda:self.button_click(self.button_1))self.button_2.clicked.connect(lambda:self.button_click(self.button_2))self.h_layout = QHBoxLayout()self.v_layout = QVBoxLayout()self.h_layout.addWidget(self.button_1)self.h_layout.addWidget(self.button_2)self.v_layout.addWidget(self.text)self.v_layout.addLayout(self.h_layout)self.setLayout(self.v_layout)def button_click(self,button):if button == self.button_1:if choice == QMessageBox.Yes:with open('text_edit.txt','w') as f:f.write(self.text.toPlainText())self.close()elif choice == QMessageBox.No:self.close()elif button == self.button_2:print("文本被清除")self.text.clear()if __name__== "__main__":app = QApplication(sys.argv)test = Simple_Notebook()test.show()sys.exit(app.exec())
ouput:
QTextBrowser
介绍
QTextBrowser类为浏览富文本提供了超文本导航。
调用append方法想文本浏览器中添加文本
import sys
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton,QTextEdit,QVBoxLayout,QHBoxLayout,QMessageBox
from PyQt5.QtGui import QIconclass Simple_Notebook(QWidget):def __init__(self):super(Simple_Notebook,self).__init__() #使用super函数可以实现子类使用父类的方法self.setWindowTitle("记事本")self.setWindowIcon(QIcon('NoteBook.png'))self.resize(500,500)self.text = QTextBrowser(self)self.text.setText("Have a nice day")self.text.setPlaceholderText("Please add some here") #设置文本初始化内容,调用setReadOnly方法并传入False参数即可编辑浏览器框self.text.textChanged.connect(lambda:print("it is changed"))self.button_1 = QPushButton("Save",self)self.button_2 = QPushButton("Clear",self)self.button_3 = QPushButton("Add",self)self.button_1.clicked.connect(lambda:self.button_click(self.button_1))self.button_2.clicked.connect(lambda:self.button_click(self.button_2))self.button_3.clicked.connect(self.add_text)self.h_layout = QHBoxLayout()self.v_layout = QVBoxLayout()self.h_layout.addWidget(self.button_1)self.h_layout.addWidget(self.button_2)self.h_layout.addWidget(self.button_3)self.v_layout.addWidget(self.text)self.v_layout.addLayout(self.h_layout)self.setLayout(self.v_layout)def button_click(self,button):if button == self.button_1:if choice == QMessageBox.Yes:with open('text_edit.txt','w') as f:f.write(self.text.toPlainText())self.close()elif choice == QMessageBox.No:self.close()elif button == self.button_2:print("文本被清除")self.text.clear()def add_text(self):self.text.append("hello,world") #调用append方法if __name__== "__main__":app = QApplication(sys.argv)test = Simple_Notebook()test.show()sys.exit(app.exec())
output:
希望这篇文章可以帮助到大家,感谢大家的支持。
这篇关于PyQt5入门学习笔记(五)—QTextEdit、QTextBrowser的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!