1. 程式人生 > 程式設計 >pyqt5 textEdit、lineEdit操作的示例程式碼

pyqt5 textEdit、lineEdit操作的示例程式碼

1.定義一個textEdit/lineEdit:(lineEdit只需要將程式碼中的QTextEdit改為QLineEdit)

  self.textEdit = QtWidgets.QTextEdit(Dialog)
  self.textEdit.setGeometry(QtCore.QRect(70,90,171,391))
  self.textEdit.setObjectName("textEdit")
  self.textEdit.setReadOnly(True)#設定為只讀,即可以在程式碼中向textEdit裡面輸入,但不能從介面上輸入,沒有這行程式碼即可以從介面輸入

2.從程式碼中將字串顯示到textEdit:

str='要顯示的字串'
self.textEdit.setText(str)

3.追加字串:

 str='要顯示的字串'
 self.textEdit_2.append(str)

4.顯示數字到textEdit:數字必須要轉換成字串

count=10
str=str(count)
self.textEdit.setText(str)

5.讀取textEdit中的文字:textEdit和LineEdit中的文字讀取方法是不一樣的

str1 = self.textEdit.toPlainText()
#textEdit 用toPlainText()方法
#linEdit 直接用self.lineEdit.text()即可獲取

PyQt5 QTextEdit控制元件操作

from PyQt5.Qt import *
import sys
import math

#超連結
class MyTextEdit(QTextEdit):
  def mousePressEvent(self,me):
    print(me.pos())
    link_str=self.anchorAt(me.pos())
    if(len(link_str)>0):
      QDesktopServices.openUrl(QUrl(link_str))
    return super().mousePressEvent(me)

class Window(QWidget):
  def __init__(self):
    super().__init__()
    self.setWindowTitle("QTextEdit的學習")
    self.resize(500,500)
    self.setWindowIcon(QIcon("D:\ICO\ooopic_1540562292.ico"))
    self.setup_ui()
  def setup_ui(self):
    te=MyTextEdit(self)
    self.te=te
    te.move(100,100)
    te.resize(300,300)
    te.setStyleSheet("background-color:cyan;")

    but=QPushButton(self)
    but.move(50,50)
    but.setText("測試按鈕")
    #self.佔位文字的提示()
    self.文字內容的設定()
    #self.格式設定和合並()
    but.pressed.connect(self.but_test)
    #te.textCursor().insertTable(5,3)
    #te.insertHtml("xxx"*300+"<a name='lk' href='#itlike'>撩課</a>"+"aaa"*200)
    te.insertHtml("xxx"*300+"<a href='http://www.itlike.com'>撩課</a>"+"aaa"*200)

    te.textChanged.connect(self.text_change)#文字發生改變
    te.selectionChanged.connect(self.selection_change)#選中的文字發生改變
    te.copyAvailable.connect(self.copy_a)#複製是否可用
  def copy_a(self,yes):
    print("複製是否可用",yes)

  def selection_change(self):
    print("文字選中的內容發生了改變")

  def text_change(self):
    print("文字內容發生了改變")

  def but_test(self):
    #self.te.clear()
    #self.游標插入內容()
    #self.內容和格式的獲取()
    #self.字型設定()
    #self.顏色設定()
    #self.字元設定()
    #self.常用編輯操作()
    #self. 只讀設定()
    #self.AB功能測試()
    self.開啟超連結()

  def 開啟超連結(self):
    pass
  def AB功能測試(self):
    #self.te.setTabChangesFocus(True)
    print(self.te.tabStopDistance())
    self.te.setTabStopDistance(100)

  def 只讀設定(self):
    self.te.setReadOnly(True)
    self.te.insertPlainText("itlike")

  def 滾動到錨點(self):
    self.te.scrollToAnchor("lk")

  def 常用編輯操作(self):
    #self.te.copy()
    #self.te.paste()
    #self.te.selectAll()
    #self.te.setFocus()
    #QTextDocument.FindBackward
    print(self.te.find("xx",QTextDocument.FindBackward|QTextDocument.FindCaseSensitively))
    self.te.setFocus()

  def 字元設定(self):
    tcf=QTextCharFormat()
    tcf.setFontFamily("宋體")
    tcf.setFontPointSize(20)
    tcf.setFontCapitalization(QFont.Capitalize)
    tcf.setForeground(QColor(100,200,150))
    self.te.setCurrentCharFormat(tcf)
    tcf2=QTextCharFormat()
    tcf2.setFontOverline(True)
    #self.te.setCurrentCharFormat(tcf2)
    self.te.mergeCurrentCharFormat(tcf2)

  def 顏色設定(self):
    self.te.setTextBackgroundColor(QColor(200,10,10))
    self.te.setTextColor(QColor(10,10))

  def 字型設定(self):
    #QFontDialog.getFont()
    self.te.setFontFamily("幼圓")
    self.te.setFontWeight(QFont.Black)
    self.te.setFontItalic(True)
    self.te.setFontPointSize(30)
    self.te.setFontUnderline(True)
    #font=QFont()
    #font.setStrikeOut(True)
    #self.te.setCurrentFont(font)


  def 對齊方式(self):
    self.te.setAlignment(Qt.AlignCenter)

  def 游標設定(self):
    print(self.te.cursorWidth())
    if self.te.overwriteMode():
      self.te.setOverwriteMode(False)
      self.te.setCursorWidth(1)
    else:
      self.te.setOverwriteMode(True)
      self.te.setCursorWidth(10)
  def 覆蓋模式的設定(self):
    self.te.setOverwriteMode(True)
    print(self.te.overwriteMode())

  def 軟換行模式(self):
    #self.te.setLineWrapMode(QTextEdit.NowWrap)
    #self.te.setLineWrapMode(QTextEdit.FixedPixelWidth)
    self.te.setLineWrapMode(QTextEdit.FixedColumnWidth)
    self.te.setLineWrapColumnOrWidth(8)
  def 自動格式化(self):
    QTextEdit
    self.te.setAutoFormatting(QTextEdit.AutoBulletList)#錄入*號自動產生格式
  def 開始和結束編輯塊(self):
    tc=self.te.textCursor()
    #tc.beginEditBlock()
    tc.insertText("123")
    tc.insertBlock()
    tc.insertText("456")
    tc.insertBlock()
    #tc.cndEditBlock()

    tc.insertText("789")
    tc.insertBlock()
  def 位置相關(self):
    tc=self.te.textCursor()#獲取游標
    print("是否在段落的結尾",tc.atBlockEnd)
    print("是否在段落的開始",tc.atBlockStart())
    print("是否在文件的結尾",tc.atEnd())
    print("是否在文件的開始",tc.atStart())
    print("在第幾列",tc.columnNumber())
    print("游標位置",tc.position())
    print("在文字塊中的位置",tc.positionInBlock())
  def 文字字元的刪除(self):
    tc=self.te.textCursor()
    #tc.deleteChar()#向右側清除
    tc.deletePreviousChar()#向左側清除
    self.te.setFocus()
  def 文字的其他操作(self):
    tc=self.te.textCursor()
    #print(tc.selectionStart())#獲取選中起始
    #print(tc.selectionEnd())#獲取選中結束
    #tc.clearSelection()#清除選中
    #self.te.setTextCursor()#設定游標
    #print(tc.hasSelection())
    tc.removeSelectedText()
    self.te.setFocus()
  def 文字選中內容的獲取(self):
    tc=self.te.textCursor()
    print(tc.selectedText())
    QTextDocumentFragment
    print(tc.selection().toPlainText())
    print(tc.selectedTableCells())
  def 文字選中和清空(self):
    tc=self.te.textCursor()
    #tc.setPosition(6,QTextCursor,KeepAnchor)
    #tc.movePosition(QTextCursor.Up,QTextCursor.KeepAnchor,1)
    tc.select(QTextCursor.WordUnderCursor)
    self.te.setTextCursor(tc)

  def 格式設定和合並(self):
    #設定上下間距
    tc=self.te.textCursor()
    tcf=QTextCharFormat()
    tcf.setFontFamily("幼圓")
    tcf.setFontPointSize(30)
    tcf.setFontOverline(True)
    tcf.setFontUnderline(True)
    tc.setCharFormat(tcf)
    return None

    #設定上下劃線及字型大小
    tc=self.te.textCursor()
    tcf=QTextCharFormat()
    tcf.setFontFamily("幼圓")
    tcf.setFontPointSize(30)
    tcf.setFontOverline(True)
    tcf.setFontUnderline(True)
    tc.setBlockCharFormat(tcf)
    pass

  def 內容和格式的獲取(self):
    tc=self.te.textCursor()
    QTextLine
    print(tc.block().text())
    print(tc.blockNumber())
    #print(tc.currentList().count())
    pass
  def 文字內容的設定(self):
    #設定普通文字內容
    self.te.setPlainText("<h1>ooo</h1>")
    self.te.insertPlainText("<h1>ooo</h1>")
    print(self.te.toPlainText())
    #富文字的操作
    self.te.setHtml("<h1>ooo</h1>")
    self.te.insertHtml("<h6>社會我的順哥</h6>")
    print(self.te.toHtml())

  def 佔位文字的提示(self):
    self.te.setPlaceholderText("請輸入你的個人簡介")

  def 游標插入內容(self):
    tc=self.te.textCursor()#獲取焦點
    tff=QTextFrameFormat()
    tff.setBorder(10)
    tff.setBorderBrush(QColor(100,50,50))
    tff.setRightMargin(50)
    tc.insertFrame(tff)
    doc=self.te.document()
    root_frame=doc.rootFrame()
    root_frame.setFrameFormat()
    return None
    tc=self.te.textCursor()#獲取游標
    tbf=QTextBlockFormat()
    tcf=QTextCharFormat()
    tcf.setFontFamily("隸書")
    tcf.setFontItalic(True)
    tcf.setFontPointSize(20)
    tbf.setAlignment(Qt.AlignRight)#對齊
    tbf.setRightMargin(100)
    tc.insertBlock(tbf,tcf)
    self.te.setFocus()#焦點
    return None
    #建立或插入新增表格
    tc=self.te.textCursor()
    ttf=QTextTableFormat()
    ttf.setAlignment(Qt.AlignRight)
    ttf.setCellPadding(6)
    ttf.setCellSpacing(13)


    ttf.setColumnWidthConstraints((QTextLength(QTextLength.PercentageLength,50),QTextLength(QTextLength.PercentageLength,40),10)))#單元格長度比例

    table=tc.insertTable(5,3,ttf)
    table.appendColumns(2)
    return None

    #設定對齊
    tc=self.te.textCursor()
    #tl=tc.insertList(QTextListFormat.ListCircle)
    #tl=tc.insertList(QTectListFormat.ListDecimal)
    #tl=tc.createList(QTextListFormat.ListDecimal)
    tlf=QTextListFormat()
    tlf.setIndent(3)
    tlf.setNumberPrefix("<<")
    tlf.setNumberSuffix("<<")
    tlf.setStyle(QTextListFormat.ListDecimal)
    tl=tc.createList(tlf)
    QTextList
    return None

    #插入普通文字或者富文字
    tc=self.te.textCursor()
    tdf=QTextDocumentFragment.fromHtml("<h1>xxx</h1>")
    #tdf=QTextDocumentFragment.fromPlainText("<h1>xxx</h1>")
    tc.insertFragment(tdf)
    return None
    #插入圖片
    tc=self.te.textCursor()
    tif=QTextImageFormat()
    tif.setName("D:\ICO\ooopic_1517621187.ico")
    tif.setWidth(100)
    tif.setHeight(100)
    tc.insertImage("D:\ICO\mmmmm.JPG")

    return None
    #插入接
    QTextCursor
    tcf=QTextCharFormat()
    tcf.setToolTip("撩課學院網址")
    tcf.setFontFamily("隸書")
    tcf.setFontPointSize(12)
    tc=self.te.textCursor()
    tc.insertText("itlike.com",tcf)
    tc.insertHtml("<a href='http://www.itlike.com'>撩課</a>")

if __name__=="__main__":
  App=QApplication(sys.argv)
  Win=Window()
  Win.show()
  sys.exit(App.exec_())

到此這篇關於pyqt5 textEdit、lineEdit操作的示例程式碼的文章就介紹到這了,更多相關pyqt5 textEdit、lineEdit操作內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!