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)#設(shè)置為只讀,即可以在代碼中向textEdit里面輸入,但不能從界面上輸入,沒有這行代碼即可以從界面輸入
2.從代碼中將字符串顯示到textEdit:
str='要顯示的字符串' self.textEdit.setText(str)
3.追加字符串:
str='要顯示的字符串' self.textEdit_2.append(str)
4.顯示數(shù)字到textEdit:數(shù)字必須要轉(zhuǎn)換成字符串
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的學(xué)習(xí)")
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.文本內(nèi)容的設(shè)置()
#self.格式設(shè)置和合并()
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 *200)
te.textChanged.connect(self.text_change)#文本發(fā)生改變
te.selectionChanged.connect(self.selection_change)#選中的文本發(fā)生改變
te.copyAvailable.connect(self.copy_a)#復(fù)制是否可用
def copy_a(self,yes):
print("復(fù)制是否可用",yes)
def selection_change(self):
print("文本選中的內(nèi)容發(fā)生了改變")
def text_change(self):
print("文本內(nèi)容發(fā)生了改變")
def but_test(self):
#self.te.clear()
#self.光標(biāo)插入內(nèi)容()
#self.內(nèi)容和格式的獲取()
#self.字體設(shè)置()
#self.顏色設(shè)置()
#self.字符設(shè)置()
#self.常用編輯操作()
#self. 只讀設(shè)置()
#self.AB功能測試()
self.打開超鏈接()
def 打開超鏈接(self):
pass
def AB功能測試(self):
#self.te.setTabChangesFocus(True)
print(self.te.tabStopDistance())
self.te.setTabStopDistance(100)
def 只讀設(shè)置(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 字符設(shè)置(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 顏色設(shè)置(self):
self.te.setTextBackgroundColor(QColor(200,10,10))
self.te.setTextColor(QColor(10,200,10))
def 字體設(shè)置(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 光標(biāo)設(shè)置(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 覆蓋模式的設(shè)置(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)#錄入*號自動產(chǎn)生格式
def 開始和結(jié)束編輯塊(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 位置相關(guān)(self):
tc=self.te.textCursor()#獲取光標(biāo)
print("是否在段落的結(jié)尾",tc.atBlockEnd)
print("是否在段落的開始",tc.atBlockStart())
print("是否在文檔的結(jié)尾",tc.atEnd())
print("是否在文檔的開始",tc.atStart())
print("在第幾列",tc.columnNumber())
print("光標(biāo)位置",tc.position())
print("在文本塊中的位置",tc.positionInBlock())
def 文本字符的刪除(self):
tc=self.te.textCursor()
#tc.deleteChar()#向右側(cè)清除
tc.deletePreviousChar()#向左側(cè)清除
self.te.setFocus()
def 文本的其他操作(self):
tc=self.te.textCursor()
#print(tc.selectionStart())#獲取選中起始
#print(tc.selectionEnd())#獲取選中結(jié)束
#tc.clearSelection()#清除選中
#self.te.setTextCursor()#設(shè)置光標(biāo)
#print(tc.hasSelection())
tc.removeSelectedText()
self.te.setFocus()
def 文本選中內(nèi)容的獲取(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 格式設(shè)置和合并(self):
#設(shè)置上下間距
tc=self.te.textCursor()
tcf=QTextCharFormat()
tcf.setFontFamily("幼圓")
tcf.setFontPointSize(30)
tcf.setFontOverline(True)
tcf.setFontUnderline(True)
tc.setCharFormat(tcf)
return None
#設(shè)置上下劃線及字體大小
tc=self.te.textCursor()
tcf=QTextCharFormat()
tcf.setFontFamily("幼圓")
tcf.setFontPointSize(30)
tcf.setFontOverline(True)
tcf.setFontUnderline(True)
tc.setBlockCharFormat(tcf)
pass
def 內(nèi)容和格式的獲取(self):
tc=self.te.textCursor()
QTextLine
print(tc.block().text())
print(tc.blockNumber())
#print(tc.currentList().count())
pass
def 文本內(nèi)容的設(shè)置(self):
#設(shè)置普通文本內(nèi)容
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 光標(biāo)插入內(nèi)容(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()#獲取光標(biāo)
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
#創(chuàng)建或插入添加表格
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
#設(shè)置對齊
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("撩課學(xué)院網(wǎng)址")
tcf.setFontFamily("隸書")
tcf.setFontPointSize(12)
tc=self.te.textCursor()
tc.insertText("itlike.com",tcf)
tc.insertHtml("<a )
if __name__=="__main__":
App=QApplication(sys.argv)
Win=Window()
Win.show()
sys.exit(App.exec_())
到此這篇關(guān)于pyqt5 textEdit、lineEdit操作的示例代碼的文章就介紹到這了,更多相關(guān)pyqt5 textEdit、lineEdit操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
13行python代碼實現(xiàn)對微信進行推送消息的示例代碼
本文主要介紹了13行python代碼實現(xiàn)對微信進行推送消息的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

