1、QTextEdit简介
是一个高级的查看器/编辑器,支持使用HTML样式标签的高文本格式;
可以处理大型文档并快速响应用户输入;
适用于段落和字符;
文本编辑可以加载纯文本和高文本文件。
2、功能作用——占位文本设置
(1)框架
(2)功能操作及展示
1 # 占位文本提示 2 def 占用文本的提示(self): 3 self.te.setPlaceholderText('请输入你的个人简介') 4 print(self.te.placeholderText())
3、功能作用——内容设置
(1)框架
(2)功能操作及展示:普通文本、HTML、设置文本、追加文本、清空
1 from PyQt5.Qt import * 2 3 class Window(QWidget): 4 def __init__(self): 5 super().__init__() 6 self.setWindowTitle("QTextEdit") 7 self.resize(500, 500) 8 self.setup_ui() 9 10 def setup_ui(self): 11 te = QTextEdit("xx",self) 12 self.te = te 13 te.move(50,50) 14 te.resize(300,300) 15 te.setStyleSheet('background-color:cyan') 16 # self.占用文本的提示() 17 self.文本内容的设置() 18 19 20 test_btn = QPushButton(self) 21 test_btn.move(10,10) 22 test_btn.setText("测试按钮") 23 test_btn.pressed.connect(self.btn_test) 24 25 def btn_test(self): 26 # 清空设置 27 self.te.setText("") 28 self.te.clear() 29 30 print(self.te.document()) 31 print(self.te.textCursor()) 32 33 34 def 文本内容的设置(self): 35 # 设置普通文本内容 36 # self.te.setPlainText("<h1>xxx</h1>") 37 # self.te.insertPlainText("<h1>xxx</h1>") # 不覆盖至之前的内容,在光标处插入 38 # print(self.te.toPlainText()) # 获取内容 39 40 # 富文本操作 41 # self.te.setHtml("<h1>xxx</h1>") 42 # self.te.insertHtml("<h2>牛逼<h2/>") 43 # print(self.te.toHtml()) 44 45 # 设置文本(自动判定) 46 # self.te.setText("<h1>ooo</h1>") 47 48 # 追加文本(自动识别) 49 self.te.append("<h3>www<h3/>") # 在文本的末尾追加 50 51 # 占位文本提示 52 def 占用文本的提示(self): 53 self.te.setPlaceholderText('请输入你的个人简介') 54 55 56 if __name__ == '__main__': 57 import sys 58 59 app=QApplication(sys.argv) 60 61 window=Window() 62 window.show() 63 sys.exit(app.exec_())
(3)文本光标
① 添加内容
1 # *********插入框架***********begin 2 tc = self.te.textCursor() 3 tff = QTextFrameFormat() 4 tff.setBorder(10) 5 tff.setBorderBrush(QColor(100,50,50)) 6 tff.setRightMargin(50) 7 tc.insertFrame(tff) 8 9 doc = self.te.document() 10 root_frame = doc.rootFrame() # 嵌入另一个框架 11 root_frame.setFrameFormat(tff) 12 13 return None 14 # *********插入框架***********end 15 16 # *********插入文本块***********begin 17 tc = self.te.textCursor() 18 tbf = QTextBlockFormat() 19 tcf = QTextCharFormat() 20 tcf.setFontFamily('隶书') 21 tcf.setFontItalic(True) # 设置肢体倾斜 22 tcf.setFontPointSize(30) 23 tbf.setAlignment(Qt.AlignRight) 24 tbf.setRightMargin(100) 25 # tbf.setIndent(3) 26 tc.insertBlock(tbf,tcf) # 加入段落格式与文本格式 27 self.te.setFocus() 28 29 return None 30 # *********插入文本块***********end 31 32 # *********插入表格***********begin 33 tc = self.te.textCursor() 34 # tc.insertTable(5,3) 35 ttf = QTextTableFormat() 36 ttf.setAlignment(Qt.AlignRight) # 右对齐 37 ttf.setCellPadding(6) # 内边距 38 ttf.setCellSpacing(3) # 外边距 39 ttf.setColumnWidthConstraints((QTextLength(QTextLength.PercentageLength,50),\ 40 QTextLength(QTextLength.PercentageLength,30),\ 41 QTextLength(QTextLength.PercentageLength,10))) # 宽度的约束 42 table = tc.insertTable(5,3,ttf) 43 table.appendColumns(2) # 增加2列 44 return None 45 # *********插入表格***********end 46 47 # *********插入列表2***********begin 48 tc = self.te.textCursor() 49 tlf = QTextListFormat() 50 tlf.setIndent(3) # 缩进3个tab键 51 tlf.setNumberPrefix("<<<") # 前缀 52 tlf.setNumberSuffix(">>>") # 后缀 53 tlf.setStyle(QTextListFormat.ListDecimal) 54 tl = tc.createList(tlf) 55 print(tl) 56 return None 57 # *********插入列表2***********end 58 # *********插入列表1***********begin 59 tc = self.te.textCursor() 60 # tl = tc.insertList(QTextListFormat.ListCircle) # 列表的左边为圆圈 61 # tl = tc.insertList(QTextListFormat.ListDecimal) # 列表的左边为数字 62 tl = tc.createList(QTextListFormat.ListDecimal) # 创建(无论光标在哪儿,都将其变成一行) 63 print(tl) 64 return None 65 # *********插入列表1***********end 66 67 # *********插入句子***********begin 68 tc = self.te.textCursor() 69 tdf = QTextDocumentFragment.fromHtml("<h1>xxx<h1/>") 70 tc.insertFragment(tdf) 71 return None 72 # *********插入句子***********end 73 74 # *********插入图片***********begin 75 tc = self.te.textCursor() 76 tif = QTextImageFormat() 77 tif.setName('xxx.png') 78 tif.setWidth(100) 79 tif.setHeight(100) 80 # tc.insertImage(tif) # 插入图片,记住这一种 81 # tc.insertImage(tif,QTextFrameFormat.FloatRight) # 插入图片靠右 82 tc.insertImage('close.png') # 通过字符串插入图片 83 return None 84 # *********插入图片***********end 85 86 # *********插入文本与超链接***********begin 87 tcf = QTextCharFormat() 88 tcf.setToolTip("fxb博客地址") 89 tcf.setFontFamily('隶书') 90 tcf.setFontPointSize(20) 91 tc = self.te.textCursor() 92 tc.insertText("itlike.com", tcf) # 插入文本,并修改格式 93 94 tc.insertHtml("<a href='http://52studyit.com'>hello</a>") # 插入超链接 95 # *********插入文本与超链接***********end