pyqt5 textEdit、lineEdit操作的示例代码


Posted in Python onAugust 12, 2020

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("<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_())

到此这篇关于pyqt5 textEdit、lineEdit操作的示例代码的文章就介绍到这了,更多相关pyqt5 textEdit、lineEdit操作内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
用Python编写脚本使IE实现代理上网的教程
Apr 23 Python
python实现xlsx文件分析详解
Jan 02 Python
python实现决策树、随机森林的简单原理
Mar 26 Python
创建pycharm的自定义python模板方法
May 23 Python
详解django中使用定时任务的方法
Sep 27 Python
将python图片转为二进制文本的实例
Jan 24 Python
python如何获取列表中每个元素的下标位置
Jul 01 Python
解决Python安装时报缺少DLL问题【两种解决方法】
Jul 15 Python
python中threading开启关闭线程操作
May 02 Python
为什么python比较流行
Jun 19 Python
python从PDF中提取数据的示例
Oct 30 Python
使用python向MongoDB插入时间字段的操作
May 18 Python
基于python requests selenium爬取excel vba过程解析
Aug 12 #Python
PyCharm+PyQt5+QtDesigner配置详解
Aug 12 #Python
Python自动发送和收取邮件的方法
Aug 12 #Python
Selenium webdriver添加cookie实现过程详解
Aug 12 #Python
Python如何设置指定窗口为前台活动窗口
Aug 12 #Python
Python面向对象实现方法总结
Aug 12 #Python
Python命名空间及作用域原理实例解析
Aug 12 #Python
You might like
ThinkPHP中url隐藏入口文件后接收alipay传值的方法
2014/12/09 PHP
PHP7.1实现的AES与RSA加密操作示例
2018/06/15 PHP
js 与 php 通过json数据进行通讯示例
2014/03/26 Javascript
jquery.ajax之beforeSend方法使用介绍
2014/12/08 Javascript
js如何打印object对象
2015/10/16 Javascript
实例详解Nodejs 保存 payload 发送过来的文件
2016/01/14 NodeJs
JS弹出新窗口被拦截的解决方法
2016/08/09 Javascript
jQuery和JavaScript节点插入元素的方法对比
2016/11/18 Javascript
js 点击a标签 获取a的自定义属性方法
2016/11/21 Javascript
本地存储localStorage用法详解
2017/07/31 Javascript
React Native如何消除启动时白屏的方法
2017/08/08 Javascript
JS实现关键词高亮显示正则匹配
2018/06/22 Javascript
微信小程序实现无限滚动列表
2020/05/29 Javascript
jQuery实现的自定义轮播图功能详解
2018/12/28 jQuery
微信小程序基于canvas渐变实现的彩虹效果示例
2019/05/03 Javascript
vant picker+popup 自定义三级联动案例
2020/11/04 Javascript
基于Python3 逗号代码 和 字符图网格(详谈)
2017/06/22 Python
python socket网络编程之粘包问题详解
2018/04/28 Python
使用tqdm显示Python代码执行进度功能
2019/12/08 Python
Python tkinter之Bind(绑定事件)的使用示例
2021/02/05 Python
css3实现动画的三种方式
2020/08/24 HTML / CSS
HTML5 DeviceOrientation实现手机网站摇一摇功能代码实例
2015/04/24 HTML / CSS
html5教你做炫酷的碎片式图片切换 (canvas)
2017/07/28 HTML / CSS
在对linux系统分区进行格式化时需要对磁盘簇(或i节点密度)的大小进行选择,请说明选择的原则
2012/01/13 面试题
心得体会怎么写
2013/12/30 职场文书
syb养殖创业计划书
2014/01/09 职场文书
深入开展党的群众路线教育实践活动方案
2014/02/04 职场文书
法学专业自我鉴定
2014/02/05 职场文书
小学清明节活动方案
2014/03/08 职场文书
质量标语大全
2014/06/12 职场文书
小学教师师德师风自我评价
2015/03/04 职场文书
酒店采购员岗位职责
2015/04/03 职场文书
主持人开场白台词
2015/05/29 职场文书
致接力运动员加油稿
2015/07/21 职场文书
儿童诗两首教学反思
2016/02/23 职场文书
解析mybatis-plus中的resultMap简单使用
2021/11/23 Java/Android