繼承 QObject-->QWidget-->QFrame-->QAbstractScrollArea-->QTextEdit
QTextEdit類是一個多行文本框控件,可以顯示多行文本內容,當文本內容超出控件顯示范圍時,可以顯示水平個垂直滾動條,Qtextedit不僅可以用來顯示文本還可以用來顯示HTML4文檔,圖像,表格
任何一個文本編輯器的程序都要用到QTextEdit作為輸入文本的容器,在它里面輸入的可編輯文本由QTextDocument作為載體
文本:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit from PyQt5.QtGui import QTextCharFormat class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit('我愛學習', self) #創建多行文本對象 #參數1 顯示的文本 #參數2 父控件 #注意 光標在0位置 t.setPlaceholderText('占位提示') #在文本框內部內容為空時, 給用戶的文本提示信息 s=t.placeholderText() #返回占位提示信息 t.setPlainText('我愛我的祖國') #設置普通文本,原來的文本被覆蓋掉 #注意 光標在0位置 t.insertPlainText(',我想為祖國做點貢獻') #在光標處插入普通文本 #會自動移動光標 s=t.toPlainText() #返回文本框的文本內容-純文本 t.setHtml('<h1>我愛我的祖國</h1>') #設置HTML文本-富文本,原來的文本被覆蓋掉 # 注意 光標在0位置 t.insertHtml('<h1>,我想為祖國做點貢獻</h1>') #在光標處插入HTML文本-富文本 # 會自動移動光標 s=t.toHtml() #返回文本框的文本內容-富文本 t.setText('<h1>我愛我的祖國</h1>') #設置文本-自動判斷是普通文本還是富文本 # 注意 光標在0位置 原來的文本被覆蓋掉 #t.append(',我想為祖國做點貢獻') #在尾部追加文本-自動采用前面的文本格式,自動判斷是普通文本還是富文本 #t.clear() #清空文本 tc=t.textCursor() #獲取文本光標對象->QTextCursor #利用文本光標對象插入文本-格式一 tc.insertText('中國人') #在光標處插入文本,自動判斷格式 #自動移動光標 # 利用文本光標對象插入文本-格式二-帶字體 tcf=QTextCharFormat() #創建文本字符格式對象 tcf.setToolTip('楷體') #當鼠標在這個字體上懸停時的提示信息 tcf.setFontFamily('李明') #設置字體 tcf.setFontPointSize(30) #設置字體大小 tc.insertText('天津',tcf) #參數2 可選-字體 # 利用文本光標對象插入HTML文本 tc.insertHtml('<h3>塘沽</h3>') # 自動移動光標 #print(s) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
利用文本光標對象插入圖片:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextCharFormat,QTextImageFormat,QTextFrameFormat class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit('我愛學習', self) t.setText('<h1>我愛我的祖國</h1>') tb=QPushButton('按鈕',self) tb.move(100,200) def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor # 利用文本光標對象插入圖片 tif = QTextImageFormat() # 創建文本圖像格式 tif.setName('大象.png') # 設置圖片 tif.setWidth(100) # 設置圖片寬度 tif.setHeight(100) # 設置圖片高度 tc.insertImage(tif,QTextFrameFormat.InFlow) #插入圖片-非環繞 #參數2 圖片位置 #QTextFrameFormat.FloatRight=2 在右邊 #QTextFrameFormat.FloatLeft=1 在左邊 #QTextFrameFormat.InFlow=0 在光標處 tb.clicked.connect(A) #print(s) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
利用文本光標對象插入文本片段:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextDocumentFragment class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit('我愛學習', self) tb=QPushButton('按鈕',self) tb.move(100,200) def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor # 利用文本光標對象插入文本片段 #tdf=QTextDocumentFragment.fromHtml('<h2>我是中國人</h2>') #創建富文本片段 tdf = QTextDocumentFragment.fromPlainText('<h2>我是中國人</h2>') # 創建普通文本片段 tc.insertFragment(tdf) #在光標處插入文本片段 #自動移動光標 t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
列表-word的項目編號和項目符號:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextListFormat class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit('我愛學習', self) tb=QPushButton('按鈕',self) tb.move(100,200) def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor # 利用文本光標對象插入列表-個人理解:word的段落編號和項目符號 #方式一 #s=tc.insertList(QTextListFormat.ListDecimal) #在當前光標處插入一個新塊,並使其成為具有給定格式的新創建列表的第一個列表項。返回創建的列表 #返回值類型QTextList #QTextListFormat.ListCircle 一個空的圓圈 #QTextListFormat.ListDisc 一個圓圈 #QTextListFormat.ListSquare 一個方塊 #QTextListFormat.ListDecimal 十進制值按升序排列 #QTextListFormat.ListLowerAlpha 小寫拉丁字符按字母順序排列 #QTextListFormat.ListUpperAlpha 大寫拉丁字符按字母順序排列 #QTextListFormat.ListLowerRoman 小寫羅馬數字(僅支持最多4999項) #QTextListFormat.ListUpperRoman 大寫羅馬數字(僅支持最多4999項) #方式二 #tc.createList(QTextListFormat.ListDecimal) #創建並返回具有給定格式的新列表,並使當前段落是第一個列表項 #方式三 tlf=QTextListFormat() tlf.setIndent(1) #縮進1個Tab tlf.setNumberPrefix('>>') #前綴-放在樣式前面 tlf.setNumberSuffix('<<') #后綴-放在樣式后面 tlf.setStyle(QTextListFormat.ListDecimal) #設置樣式 #參數 參考方式一 #只有設置了樣式,前綴后綴才有效果 tc.createList(tlf) t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
表格:
名稱:記錄--一行 字段-一列
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextTableFormat,QTextLength from PyQt5.QtCore import Qt class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor # 利用文本光標對象插入表格 #tt=tc.insertTable(3,2 ) #在光標處插入3行2列的表格-不帶格式 #返回值類型 QTextTable #表格不環繞單獨占用行 ttf=QTextTableFormat() #創建表格格式 ttf.setAlignment(Qt.AlignRight) #設置對齊方式 ttf.setCellPadding(1) #設置內邊距 ttf.setCellSpacing(1) #設置外邊距 ttf.setColumnWidthConstraints((QTextLength(QTextLength.PercentageLength, 50),QTextLength(QTextLength.PercentageLength, 40))) # 列寬限制 # 元組 #按百分比計算 tt = tc.insertTable(3, 2,ttf) #在光標處插入3行2列的表格-帶格式 # 返回值類型 QTextTable # 表格不環繞單獨占用行 tt.appendColumns(2) #追加兩列 tt.appendRows(1) #追加1行 t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
表格還有很多內容不會:如何操作表格 ??
插入文本塊-段落:
段落是以回車換行符為間隔的
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextBlockFormat,QTextCharFormat,QColor from PyQt5.QtCore import Qt class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國') def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor # 利用文本光標對象插入文本塊-段落 #tc.insertBlock() #插入文本塊-插入段落-在光標處插入回車換行 tbf=QTextBlockFormat() #創建文本塊格式對象 #這種格式是段落級別的 tbf.setAlignment(Qt.AlignLeft) #設置段落對齊方式 # Qt.AlignRight 水平靠右 # Qt.AlignLeft 水平靠左 # Qt.AlignHCenter 居中 # Qt.AlignJustify # Qt.AlignTop 垂直頂部 # Qt.AlignBottom 垂直底部 # Qt.AlignVCenter 垂直居中 # Qt.AlignBaseline # Qt.AlignCenter=Qt.AlignHCenter | Qt.AlignVCenter tbf.setRightMargin(20) #設置邊距-文本離右邊的距離 #單位 像素 tbf.setLeftMargin(20) #設置邊距-文本離左邊的距離 tbf.setBottomMargin(20) #設置邊距-文本離底邊的距離 tbf.setTopMargin(20) #設置邊距-文本離頂部的距離 tbf.setIndent(1) #縮進1個Tab tcf=QTextCharFormat() #創建文本字符格式對象 #這個格式是字符級別的 tcf.setFontFamily('隸書') #設置字體 s=tcf.font() #返回字體對象 s1=s.family() #返回字體名稱-隸書 s1=s.style() #返回字體樣式-int s1=s.bold() #返回是否加粗 tcf.setFontItalic(True) #是否傾斜 tcf.setFontPointSize(30) #字體大小 #fontPointSize() 返回字體大小 #tcf.setFontOverline(True) #是否有上划線 #fontOverline() 返回是否有上划線 #tcf.setFontStrikeOut(True) #設置刪除線 #s1=tcf.fontStrikeOut() #返回是否有刪除線 tcf.setFontUnderline(True) #設置下划線 #s1=tcf.fontUnderline() #返回是否具有下划線 tcf.setUnderlineColor(QColor(255,25,200,10)) #設置下划線顏色 #參數4 alpha沒有效果啊?? s=tcf.underlineColor() #返回下划線的顏色對象-QColor print(s.red()) #返回QColor對象中的red值 print(s.green()) # 返回QColor對象中的green值 print(s.blue()) # 返回QColor對象中的blue值 print(s.alpha()) # 返回QColor對象中的alpha值 #fontWeight #tcf.setFontWeight(500) #設置字體粗細 #s1=tcf.fontWeight() #返回字體粗細 #fontWordSpacing #tcf.setFontWordSpacing(10) #設置單詞間距 #s1=tcf.fontWordSpacing() #返回單詞間距-float #tcf.setFontLetterSpacing(200) #設置字母間距 #s = tcf.fontLetterSpacingType() #返回字母間距類型 #s1=tcf.fontLetterSpacing() #返回字母間距-float #tc.insertBlock(tbf) # 插入文本塊(帶格式) tc.insertBlock(tbf,tcf) # 插入文本塊(帶格式) #在文本中間插入tcf好像沒有效果 t.setFocus() print(s1) tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
框架:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextFrameFormat,QColor class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國') def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor # 利用文本光標對象插入文本框架 tff=QTextFrameFormat() #創建文本框架對象 tff.setBorder(10) #設置邊框的寬度,單位:像素 tff.setBorderBrush(QColor(255,0,0)) #設置框架下邊框和右邊框的顏色 tff.setRightMargin(10) #文本框架離右邊的間距 tc.insertFrame(tff) #插入文本框架 #返回值類型 QTextFrame doc=t.document() #獲取文本框的文檔 root_frame=doc.rootFrame() #獲取文檔的根框架 root_frame.setFrameFormat(tff) #給框架設置格式 tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
格式設置和合並:
設置塊字符格式:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextFrameFormat,QColor,QTextCharFormat class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國') def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor tcf=QTextCharFormat() tcf.setFontFamily('幼圓') tcf.setFontPointSize(30) tcf.setFontUnderline(True) tc.setBlockCharFormat(tcf) #設置當前塊(或選擇中包含的所有塊)的塊char格式-【設置當前段落的字符格式】 #光標在文本中間好像不行 tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
設置塊格式[段落格式]:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextBlockFormat from PyQt5.QtCore import Qt class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國') def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor tbf=QTextBlockFormat() tbf.setAlignment(Qt.AlignCenter) tc.setBlockFormat(tbf) #設置當前塊的塊格式(或選擇中包含的所有塊)以進行格式化 #設置當前段落格式(或選擇中包含的所有段落) t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
設置當前(選中)字符格式:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextCharFormat class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國') def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor tcf=QTextCharFormat() tcf.setFontFamily('幼圓') tcf.setFontPointSize(30) tcf.setFontUnderline(True) tc.setCharFormat(tcf) #將光標的當前字符格式設置為給定格式。如果光標有選擇,則給定格式應用於當前選擇 t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
合並格式:
import sys
from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton
from PyQt5.QtGui import QTextCharFormat
class Demo(QWidget):
def __init__(self):
super().__init__()
self.resize(300,300)
t = QTextEdit(self)
tb=QPushButton('按鈕',self)
tb.move(100,200)
t.setText('我愛祖國')
def A():
tc = t.textCursor() # 獲取文本光標對象->QTextCursor
tcf=QTextCharFormat()
tcf.setFontFamily('幼圓')
tcf.setFontPointSize(30)
tcf.setFontUnderline(True) #下划線
tc.setCharFormat(tcf)
tcf1 = QTextCharFormat()
tcf1.setFontStrikeOut(True) #刪除線
tc.mergeCharFormat(tcf1) #合並當前字符格式-->在原來字符格式的基礎上再加上新格式tcf1
#tc.mergeBlockCharFormat() #合並塊字符格式
#tc.mergeBlockFormat() #合並塊格式
t.setFocus()
tb.clicked.connect(A)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = Demo()
demo.show()
sys.exit(app.exec_())
獲取內容和格式相關:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextCharFormat class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國') def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor tcf=QTextCharFormat() tcf.setFontFamily('幼圓') tcf.setFontPointSize(30) tcf.setFontUnderline(True) tc.insertText('天津',tcf) s=tc.block() #獲取光標所在的文本塊 #返回值類型 QTextBlock print(s.text()) #返回文本框的內容 #天津我愛祖國 print(s.blockNumber()) #返回段落編號 #c=currentList() -> QTextList 獲取當前所在的文本列表-段落編號或項目編號 #c.couut() 返回列表的總數 #blockFormat() -> QTextBlockFormat 獲取光標所在的文本塊格式 #blockCharFormat() -> QTextCharFormat 獲取光標所在的文本塊字符格式 #charFormat() -> QTextCharFormat 獲取文本字符格式 #currentFrame() -> QTextFrame 獲取當前所在的框架 #currentTable() -> QTextTable 獲取當前的表格 t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
利用文本光標操作光標:
1.設置光標位置
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextCursor class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國我愛中華人民共和國') def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor tc.setPosition(6, QTextCursor.KeepAnchor) #設置光標位置 #參數2 錨點移動模式: #QTextCursor.MoveAnchor 將錨點移動到與光標本身相同的位置(默認) #QTextCursor.KeepAnchor 將錨固定在原處 #說明:錨點與光標之間的內容會被選中 t.setTextCursor(tc) #把文本光標方向設置回去---setPosition才有效果 t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
2.移動光標:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextCursor class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國我愛中華人民共和國') def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor tc.movePosition(QTextCursor.StartOfLine, QTextCursor.MoveAnchor) #移動光標 #參數1 移動選項: #QTextCursor.NoMove 將光標保持在原位 #QTextCursor.Start 移至文檔的開頭 #QTextCursor.StartOfLine 移動到當前行的開頭 #QTextCursor.StartOfBlock 移動到當前塊的開頭 #QTextCursor.StartOfWord 移動到當前單詞的開頭 #QTextCursor.PreviousBlock 移動到上一個塊的開頭 #QTextCursor.PreviousCharacter 移至上一個字符 #QTextCursor.PreviousWord 移到上一個單詞的開頭 #QTextCursor.Up 向上移動一行 #QTextCursor.Left 向左移動一個字符 #QTextCursor.WordLeft 向左移動一個單詞 #QTextCursor.End 移到文檔的末尾 #QTextCursor.EndOfLine 移動到當前行的末尾 #QTextCursor.EndOfWord 移到當前單詞的末尾 #QTextCursor.EndOfBlock 移動到當前塊的末尾 #QTextCursor.NextBlock 移動到下一個塊的開頭 #QTextCursor.NextCharacter 移動到下一個角色 #QTextCursor.NextWord 轉到下一個單詞。 #QTextCursor.Down 向下移動一行。 #QTextCursor.Right 向右移動一個角色。 #QTextCursor.WordRight 向右移動一個單詞。 #QTextCursor.NextCell 移動到當前表中下一個表格單元格的開頭。如果當前單元格是行中的最后一個單元格,則光標將移動到下一行中的第一個單元格 #QTextCursor.PreviousCell 移動到當前表內的上一個表格單元格的開頭。如果當前單元格是行中的第一個單元格,則光標將移動到上一行中的最后一個單元格 #QTextCursor.NextRow 移動到當前表中下一行的第一個新單元格。 #QTextCursor.PreviousRow 移動到當前表中上一行的最后一個單元格。 #參數2 錨點移動模式: #QTextCursor.MoveAnchor 將錨點移動到與光標本身相同的位置(默認) #QTextCursor.KeepAnchor 將錨固定在原處 #說明:錨點與光標之間的內容會被選中 t.setTextCursor(tc) #把文本光標方向設置回去---setPosition和movePosition才有效果 t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
3.選中:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextCursor class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國我愛中華人民共和國') def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor tc.select(QTextCursor.BlockUnderCursor) #選中 #參數: #QTextCursor.Document 選擇整個文檔。 #QTextCursor.BlockUnderCursor 選擇光標下的文本塊 #QTextCursor.LineUnderCursor 選擇光標下的文本行 #QTextCursor.WordUnderCursor 選擇光標下的單詞。如果光標未定位在可選字符串中,則不選擇任何文本 t.setTextCursor(tc) #把文本光標方向設置回去---setPosition和movePosition和select才有效果 t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
獲取選中的內容:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國我愛中華人民共和國') t.textCursor().insertTable(3, 4) def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor s=tc.selectedText() #返回選中的內容 #返回值類型 str s=tc.selection() #返回文本片段對象 #返回值類型 QTextDocumentFragment c=s.toPlainText() #把文本片段轉化成普通文本 c=s.toHtml() #把文本片段轉化成富文本 c=s.isEmpty() #是否為空 c=tc.selectedTableCells() #獲取選中的單元格 #返回值 (1, 2, 1, 2) 是個元組 #第一個值:選中單元格的起始行 #第二個值: 選中的總行數 #第三個值:選中單元格的起始列 #第四個值:選中的總列數 print(c) t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
獲取選中內容的位置、取消選中、是否有選中、刪除選中文本、刪除字符
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國我愛中華人民共和國') def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor print(tc.selectionStart()) #選中文本的光標的起始位置 print(tc.selectionEnd()) #選中文本的光標的結束位置 #tc.clearSelection() #取消文本的選中 #t.setTextCursor(tc) #必須方向設置,clearSelection才有效果 print(tc.hasSelection()) #是否有選中文本 tc.removeSelectedText() #移除選中的文本 #tc.deleteChar() #如果沒有選中文本, 刪除文本光標后一個字符;如果有選中文本, 則刪除選中文本 tc.deletePreviousChar() #如果沒有選中文本, 刪除文本光標前一個字符;如果有選中文本, 則刪除選中文本 t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
獲取、判斷光標的位置:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國我愛中華人民共和國') def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor #print(tc.atBlockEnd()) #光標是否在文本塊末尾 #print(tc.atBlockStart()) #光標是否在文本塊開始 #print(tc.atEnd()) #光標是否在文檔末尾 #atStart() 是否在文檔開始 #print(tc.columnNumber()) #光標在第幾列 #print(tc.position()) #返回光標位置 #返回整個文本框中的字符序號 print(tc.positionInBlock() ) #光標在文本塊中的位置--字符序號 t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
開始和結束編輯標識:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國我愛中華人民共和國') def A(): tc = t.textCursor() # 獲取文本光標對象->QTextCursor tc.beginEditBlock() tc.insertBlock() tc.insertText('123') tc.insertBlock() tc.insertText('456') tc.insertBlock() tc.insertText('789') tc.endEditBlock() #從beginEditBlock()到endEditBlock()之間的操作看做是一個獨立的操作 t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
自動格式化:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國我愛中華人民共和國') def A(): t.setAutoFormatting(QTextEdit.AutoBulletList) #設置自動格式化 #QTextEdit.AutoNone 不要做任何自動格式化---默認值 #QTextEdit.AutoBulletList 自動創建項目符號列表(例如,當用戶在最左側列中輸入星號('*')時,或在現有列表項中按Enter鍵 #QTextEdit.AutoAll 應用所有自動格式。目前僅支持自動項目符號列表。 t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
軟換行模式:--文本內容超過一行時如何處理
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextOption class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國我愛中華人民共和國') def A(): t.setLineWrapMode(QTextEdit.FixedColumnWidth) #設置軟換行模式 #QTextEdit.NoWrap 沒有軟換行, 超過寬度后, 會產生水平滾動條 #QTextEdit.WidgetWidth 以控件的寬度為限制,但會保持單詞的完整性 #QTextEdit.FixedPixelWidth 填充像素寬度---超過這個像素寬度就軟換行,配合setLineWrapColumnOrWidth(int)使用 #QTextEdit.FixedColumnWidth=3 填充列的寬度--超過這個列數就換行,配合 setLineWrapColumnOrWidth(int) s=t.lineWrapMode() #返回軟換行模式--int t.setLineWrapColumnOrWidth(10) #設置像素寬度或列數 #lineWrapColumnOrWidth() -> int 返回像素寬度或列數 t.setWordWrapMode(QTextOption.WordWrap) #設置單詞換行模式 #QTextOption.NoWrap 文本根本沒有包裝。 #QTextOption.WordWrap=1 保持單詞完整性 #QTextOption.ManualWrap 與QTextOption.NoWrap相同 #QTextOption.WrapAnywhere 寬度夠了之后, 隨意在任何位置換行 #QTextOption.WrapAtWordBoundaryOrAnywhere 盡可能趕在單詞的邊界, 否則就在任意位置換行 s=t.wordWrapMode() #-> QTextOption.WrapMode 獲取單詞換行模式 print(s) t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
覆蓋或插入:
t.setOverwriteMode(True) #設置是否覆蓋模式
s=t.overwriteMode() #覆蓋模式返回True,插入模式返回False
光標寬度:
t.setCursorWidth(int) 設置光標寬度
s=t.cursorWidth() #返回光標寬度
s=t.cursorRect() #返回光標矩形
#QRect(4, 4, 1, 14) 第3 第4 是光標的寬度和高度
段落對齊方式:
t.setAlignment(Qt.AlignLeft) #設置段落對齊方式
#Qt.AlignLeft 左對齊
#Qt.AlignRight 右對齊
#Qt.AlignCenter 居中對齊
字體格式:
s=QFontDialog.getFont() #打開字體對話框
# 返回值 (<PyQt5.QtGui.QFont object at 0x000000D12B5BE5F8>, False)
#第一個數:字體對象QFont; 第二個數:True 點擊的是確定按鈕 False 點擊的是取消按鈕
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QFont class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國我愛中華人民共和國') def A(): t.setFontFamily('黑體') #設置字體 s=t.fontFamily() #返回字體【黑體】 t.setFontPointSize(20) #字體大小 s=t.fontPointSize() #返回字體大小 #20.0 t.setFontWeight(QFont.Thin) #設置字體粗細 #枚舉值 從小到大:QFont.Thin=0 QFont.ExtraLight QFont.Light #QFont.Normal QFont.Medium QFont.DemiBold #QFont.Bold QFont.ExtraBold QFont.Black s=t.fontWeight() #返回字體粗細 t.setFontItalic(True) #設置是否斜體 s=t.fontItalic() #返回是否斜體 t.setFontUnderline(True) #設置是否下划線 s=t.fontUnderline() #返回是下划線 font=QFont() #創建字體實例 font.setStrikeOut(True) #刪除線 t.setCurrentFont(font) #統一設置字體格式 print(s) t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
顏色設置:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QColor class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國我愛中華人民共和國') def A(): #t.setTextBackgroundColor(QColor(255,0,0)) #設置文本背景顏色 #s=t.textBackgroundColor() #返回文本背景顏色對象 QColor #<PyQt5.QtGui.QColor object at 0x000000E6A6C9E048> t.setTextColor(QColor(255,0,0)) #設置文本顏色 s=t.textColor() #返回文本顏色對象 QColor print(s) t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
字符格式和合並:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QColor,QTextCharFormat class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國我愛中華人民共和國') def A(): tcf=QTextCharFormat() tcf.setFontFamily('幼圓') tcf.setFontPointSize(30) t.setCurrentCharFormat(tcf) #給字符設置格式 tcf.setFontUnderline(True) t.mergeCurrentCharFormat(tcf) #合並當前字符格式-->在原來字符格式的基礎上再加上新格式tcf t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
大小寫格式:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QColor,QTextCharFormat,QFont class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('我愛祖國我愛中華人民共和國') def A(): tcf=QTextCharFormat() tcf.setFontPointSize(20) tcf.setFontCapitalization(QFont.Capitalize) #設置大小寫 #QFont.MixedCase 這是正常的文本呈現選項,不應用大寫更改。 #QFont.AllUppercase 全大寫類型呈現的文本。 #QFont.AllLowercase 全小寫類型呈現的文本。 #QFont.SmallCaps 以小型大寫字母呈現的文本。 #QFont.Capitalize 每個單詞首字母大寫 tcf.setForeground(QColor(255,0,0)) #設置前景色 t.setCurrentCharFormat(tcf) #給字符設置格式 t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
常用編輯操作:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextDocument class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.setText('dse abc ABC abcd AbC 我愛祖國 jhg abc kwd') def A(): #t.copy() #復制 #t.paste() #粘貼 #print(t.canPaste()) #能否粘貼 返回值 bool #t.setUndoRedoEnabled(True) #??? #t.redo() #重做--取消撤銷操作 #t.setUndoRedoEnabled(False) #禁止撤銷和重做 #t.undo() #撤銷 #t.selectAll() #全選 s=t.find('abc',QTextDocument.FindBackward | QTextDocument.FindCaseSensitively | QTextDocument.FindWholeWords) #查找 #找到返回True;沒找到返回False #QTextDocument.FindBackward 向后搜索而不是向前搜索---默認 #QTextDocument.FindCaseSensitively 區分大小寫;默認是不區分 #QTextDocument.FindWholeWords 匹配僅完整的單詞 print(s) t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
滾動到錨點:
import sys from PyQt5.QtWidgets import QApplication, QWidget,QTextEdit,QPushButton from PyQt5.QtGui import QTextDocument class Demo(QWidget): def __init__(self): super().__init__() self.resize(300,300) t = QTextEdit(self) tb=QPushButton('按鈕',self) tb.move(100,200) t.insertPlainText('dse abc ABC abcd AbC 我愛祖國\n') t.insertPlainText('abc'*300) t.insertHtml('<a name="lm" href="#錨點內容">百度</a>') #給指定的文本插入錨點 #<a name="錨點名稱" href="#錨點內容"> 百度 </a> 百度是顯示的內容 def A(): t.scrollToAnchor('lm') #滾動到錨點 #參數 錨點名稱 t.setFocus() tb.clicked.connect(A) if __name__ == '__main__': app = QApplication(sys.argv) demo = Demo() demo.show() sys.exit(app.exec_())
只讀:
t.setReadOnly(True) #只讀屬性,用戶不能編輯-代碼可以編輯
s=t.isReadOnly() #返回是否只讀
制表符:
t.setTabChangesFocus(True) #Tab鍵是改變焦點的功能,沒有制表符的功能
#默認是False 是制表符的功能
t.setTabStopDistance(10) #設置制表符的距離-默認80(像素)
#參數 浮點數
#按下Tab鍵,把光標移到離左邊界固定的距離
#setTabStopWidth(p_int) 這個也可以設置
s=t.tabStopDistance() #返回Tab的距離
#返回值 浮點數 10.0
#tabStopWidth() -> int 這個也可以返回
信號:
textChanged() 文本內容發生改變時, 發射的信號
selectionChanged() 選中內容發生改變時, 發射的信號
cursorPositionChanged() 光標位置發生改變時, 發射的信號
currentCharFormatChanged(QTextCharFormat) 當前額字符格式發生改變時, 發射的信號
copyAvailable(bool yes) 復制可用時
redoAvailable(bool available) 重做可用時
undoAvailable(bool available) 撤銷可用時
天子驕龍