pyqt 5 textEdit、line Edit操作のコード例


1.textEdit/line Editを定義します。コードのQTextEditをQLine Editに変更するだけです。

  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とLINE Editの文字の読み取り方法は違います。

str1 = self.textEdit.toPlainText()
#textEdit  toPlainText()  
#linEdit    self.lineEdit.text()    
PyQt 5 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,200,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),QTextLength(QTextLength.PercentageLength,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_())
ここでは、pyqt 5 textEdit、line Edit操作のコード例についての記事を紹介します。pyqt 5 textEdit、line Editの操作内容については、以前の文章を検索したり、下記の関連記事を見たりしてください。これからもよろしくお願いします。