這篇文章給大家分享的是有關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,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("<h2>ooo</h2>")
self.te.insertPlainText("<h2>ooo</h2>")
print(self.te.toPlainText())
#富文本的操作
self.te.setHtml("<h2>ooo</h2>")
self.te.insertHtml("<h7>社會我的順哥</h7>")
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("<h2>xxx</h2>")
#tdf=QTextDocumentFragment.fromPlainText("<h2>xxx</h2>")
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的操作方法就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。