首页 PyQt5教程 QLineEdit文本编辑控件selectedText()和setTextMargins()方法教程
pay pay
教程目录

QLineEdit文本编辑控件selectedText()和setTextMargins()方法教程

日期: 四月 26, 2023, 12:18 p.m.
栏目: PyQt5教程
阅读: 170
作者: Python自学网-村长

摘要: QLineEdit文本编辑控件selectedText()和setTextMargins()方法教程

一、selectedText()方法

selectedText()方法用于获取QLineEdit中当前选中的文本,语法为:

text = lineEdit.selectedText()

其中,text为当前选中的文本。

演示:

from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QVBoxLayout

app = QApplication([])
window = QWidget()

lineEdit = QLineEdit()
lineEdit.setText("Hello World!")
lineEdit.setSelection(6, 11)

layout = QVBoxLayout()
layout.addWidget(lineEdit)
window.setLayout(layout)

window.show()

# 获取当前选中的文本
text = lineEdit.selectedText()
print(text)

app.exec_()

运行后,输出结果为“World”。

二、setTextMargins()方法

setTextMargins()方法用于设置QLineEdit中文本的边距,语法为:

lineEdit.setTextMargins(left, top, right, bottom)

其中,left、top、right、bottom为四个方向的边距大小,单位为像素。

演示:

from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QVBoxLayout

app = QApplication([])
window = QWidget()

lineEdit = QLineEdit()
lineEdit.setTextMargins(10, 10, 10, 10)

layout = QVBoxLayout()
layout.addWidget(lineEdit)
window.setLayout(layout)

window.show()
app.exec_()

运行后,QLineEdit中的文本会与四周都留有10像素的边距。

部分文字内容为【Python自学网】原创作品,转载请注明出处!视频内容已申请版权,切勿转载!
回顶部