PyQt5_QTextEdit_文本光标_添加内容-设置格式

from PyQt5.Qt import *


class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('QTextEdit_文本光标')
        self.resize(500, 500)
        self.iniUI()

    def iniUI(self):
        te = QTextEdit(self)
        self.te = te
        te.resize(self.width() * 7 / 8, self.height() * 7 / 8)
        te.move((self.width() - te.width()) / 2, 2)
        te.setStyleSheet('background-color:cyan;font-size:20px')
        te.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)

        btn = QPushButton(self)
        self.btn = btn
        self.btn_w = self.width()/3
        self.btn_h = self.height()*3/32
        self.btn.resize(self.btn_w,self.btn_h)
        self.btn_x = (self.width() - self.btn_w) / 2
        self.btn_y = self.height()*7/8 + (self.height()/8-self.btn_h)/2
        self.btn.setText('测试按钮')
        self.btn.setStyleSheet('font-size:30px')
        self.btn.move(self.btn_x, self.btn_y)

    ##############################################文本光标的一些操作
    #
    # 现在需求:
    #           通过文本光标实现对于te中文本的操作
    #
    # 解决方法:
    #        tc = self.te.textCursor()               #获取te的文本光标
    #        tc.insertText('今天是二……',tcf)          #由 光标 插入文本( 文本内容 , 文本格式对象 )
    #
    #        tc = QTextCharFormat()                 创建一个 文本字符格式 对象
    #        tc.setFontPointSize(self.width()/20)   设置字体大小
    #        tcf.setFontFamily('隶书')               设置字体样式
    def textChar(self):
        tcf = QTextCharFormat()
        tcf.setFontPointSize(self.width() / 20)
        tcf.setToolTip('这是我写的日记')
        tcf.setFontFamily('隶书')
        tc = self.te.textCursor()
        tc.insertText('今天是二零二零年二月二日,天气比较阴沉,在家里无聊的一批',tcf)
        tc.insertHtml(" 李彦宏百度  ")

        self.btn.clicked.connect()
    #############################文本光标的一些操作




    ##############################################文本光标插入图片
    #
    # 现在需求:
    #           te使用光标tc 插入图片
    #
    # 解决方法:
    #           tc.insertImage(tif)
    #
    def textImage(self):
        tif = QTextImageFormat()
        tif.setName('aaa.png')
        tif.setWidth(self.te.width()/2)
        tif.setHeight(self.te.height()/2)

        tc = self.te.textCursor()
        tc.insertImage(tif)

    #############################文本光标插入图片


    ##############################################插入文本片段
    #
    # 现在需求:
    #           te中使用tc插入文本片段
    #
    # 解决方法:
    #           tc = te.textCursor()
    #
    #           tdf = QTextDocumentFragment()
    #           tdf_plaintext = tdf.fromPlainText()
    #           tc.insertFragment
    #           tc.insertFragment


    def textFragment(self):

          tc = self.te.textCursor()

          tdf = QTextDocumentFragment()
          tdf_plaintext = tdf.fromPlainText('普通文本片段')
          tdf_htmltext = tdf.fromHtml('

富文本片段

'
) tc.insertFragment(tdf_htmltext) tc.insertFragment(tdf_plaintext) #############################插入文本片段 ##############################################插入列表,列表即多个同级元素集合 # # 现在需求: # te 使用 tc 插入列表 # # 解决方法: # tc.insertList(tlf) # def textList(self): tc = self.te.textCursor() tlf = QTextListFormat() ##下面三行主要是加一些 缩进 前后缀,修饰外观作用 tlf.setIndent(4)#列表缩进1个Tab ##前缀后缀 只会包柱自然数 tlf.setNumberPrefix('<<')#列表的标号 前缀 (每一项的标记符号为自然数 才有效) tlf.setNumberSuffix('>>')#列表的标号 后缀 (每一项的标记符号为自然数 才有效) tlf_circle = tlf.ListCircle#列表每一项的标记符号,圆圈 tlf_square = tlf.ListSquare#列表每一项的标记符号,方块 tlf_decimal = tlf.ListDecimal#列表每一项的标记符号,自然数 ## 列表 tlf 设置 列表每一项的标记符号 # tlf.setStyle(tlf_circle) tlf.setStyle(tlf_square) # tlf.setStyle(tlf_decimal) #光标 tc 插入列表 tlf的具体对象 tc.insertList(tlf) #############################插入列表,列表即多个同级元素集合 ##############################################插入表格 # # 现在需求: # te 使用 tc 插入 表格 # # 解决方法: # tc.insertTable(5,3) 使用tc插入 5行3列的表格 # # 表格的每一行相当于一条记录,每一列相当于一个字段,字段名就是列名 def textTable(self): tc = self.te.textCursor() ttf = QTextTableFormat() ttf.setAlignment(Qt.AlignLeft)#右对齐 ttf.setCellPadding(8)#单元格内边距 ttf.setCellSpacing(10)#单元格外边距 ttf.setColumnWidthConstraints([QTextLength(QTextLength.PercentageLength,200/3),\ QTextLength(QTextLength.PercentageLength,100/6),\ QTextLength(QTextLength.PercentageLength,100/6)]) # QTextTable 列表列宽约束需要一个迭代器,元组或列表都行, # 其每个元素都是一个 QTextLength 对象, # 传递了一个长度数据类型(其实就是QTextLength的类属性,(应该在一个新类里面写了Check功能,实现了QTextLength类内部的封装性) # 另一个是具体数值 # 这里用的是百分比类型,还有具体长度VariableLength,FixedLength my_table = tc.insertTable(5,3,ttf)#光标tc插入生成表格 # my_table.appendColumns(2)#表格调用QTextTable类内部appendColumns方法 my_table.mergeCells(0,0,1,5)#合并单元格,左上坐标为(0,0)一行五列 即第一行合为整体 #############################插入表格 ##############################################使用光标插入文本块 # # 现在需求: # te使用tc插入block # # 解决方法: # tc.insertBlock(tbf,tcf) # # def textBlock(self): tc = self.te.textCursor() tbf = QTextBlockFormat() tbf.setAlignment(Qt.AlignLeft) tbf.setLeftMargin(15) tcf = QTextCharFormat() tcf.setFontPointSize(22) tcf.setFontItalic(True) tcf.setFontFamily('隶书') tc.insertBlock(tbf,tcf) #############################使用光标插入文本块 ##############################################使用光标插入文本框架 # # 现在需求: # te使用tc插入 frame # # 解决方法: # tc.insertFrame() # def textFrame(self): tc = self.te.textCursor() tff = QTextFrameFormat() tff.setWidth(self.te.width() * 2 / 3) tff.setHeight(200) tff.setBorder(10) tff.setBorderBrush(QColor(100,50,50)) tff.setLeftMargin(20) tc.insertFrame(tff) # 获取文本的根框架, 并设置框架格式为ttf self.te.document().rootFrame().setFrameFormat(tff) #############################使用光标插入文本框架 ################################################################################文本格式设置 # # 现在需求: # 上面两个例子,一个是textChar,一个是textBlock # 在te使用tc插入文本的时候,附带了一个格式参数 # 现在想要使用tc里面专门的方法单独设置格式 # 解决方法: # tc.setBlockFormat( QTextBlockFormat ) # tc.setCharFormat( QTextCharFormat ) # tc.setBlockCharFormat( QTextCharFormat ) # 注意!!!!! # tc.setCharFormat(tcf) 只能修改 选中的文本,未选中的不做修改 def textSetCharFormat(self): tc = self.te.textCursor() tcf1 = QTextCharFormat() tcf1.setFontFamily('隶书') tcf1.setFontPointSize(self.width()/20) tc.setCharFormat(tcf1) def textSetBlockCharFormat(self): tc = self.te.textCursor() tcf2 = QTextCharFormat() tcf2.setFontStrikeOut(True)#给所有的文本添加 中间横杠 tc.setCharFormat(tcf2) # 对整个block段落有效 def textSetBlockFormat(self): tc = self.te.textCursor() tbf = QTextBlockFormat() tbf.setIndent(2)#段落缩进2个Tab tbf.setLeftMargin(2) tbf.setAlignment(Qt.AlignLeft) tc.setBlockFormat(tbf) ###############################################################文本格式设置 #############################################################文本格式的合并,即 前后设置效果的叠加 def textSetMergeFormat(self): tc = self.te.textCursor() tcf1 = QTextCharFormat() tcf1.setFontFamily('隶书') tcf1.setFontPointSize(self.width()/20) tc.setCharFormat(tcf1) tcf2 = QTextCharFormat() tcf2.setFontStrikeOut(True) # 给所有的文本添加 中间横杠 tc.mergeCharFormat(tcf2) # 注意: # 1,上面因为生成格式的类为 QTextCharFormat,所以合并格式的方法为 mergeCharFormat # 2,故 生成格式的 类 必须与 合并格式的方法 一一对应 # QTextBlockFormat---mergeBlockFormat # QTextBlockCharFormat---mergeBlockCharFormat ############################################文本格式的合并,即 前后设置效果的叠加 # # 无论te插入啥,首先分为两大步: # 1,创建一个tc光标对象 tc = te.textCursor() # 2,插入啥,创建啥, # 1)想插入文本格式 创建 tcf = QTextCharFormat() # 2) 想插入图片文本 创建 tif = QTextImageFormat() # 3) 想插入文本片段 创建 tdf = QTextDocumentFragment() # 3,tc执行插入操作 # 1) tc.insertText(tcf) # 2) tc.insertImage(tif) # 3) tc.insertFragment(tdf) # if __name__ == '__main__': import sys app = QApplication(sys.argv) win = MyWindow() # win.btn.clicked.connect(win.textChar) # win.btn.clicked.connect(win.textImage) # win.btn.clicked.connect(win.textFragment) # win.btn.clicked.connect(win.textList) # win.btn.clicked.connect(win.textTable) # win.btn.clicked.connect(win.textBlock) # win.btn.clicked.connect(win.textFrame) # win.btn.clicked.connect(win.textSetCharFormat) # win.btn.clicked.connect(win.textSetBlockCharFormat) # win.btn.clicked.connect(win.textSetBlockFormat) win.btn.clicked.connect(win.textSetMergeFormat) win.show() sys.exit(app.exec_())

本人学习笔记,希望大家提点意见,多多交流,感谢大家了!!
以上!

你可能感兴趣的:(PyQt5)