当前位置:网站首页>PyQy5学习(三):QLineEdit+QTextEdit
PyQy5学习(三):QLineEdit+QTextEdit
2022-04-23 05:43:00 【左小田^O^】
4.4文本框类控件
4.4.1QLineEdit
QLineEdit
类是一个单行文本框控件,可以输入单行字符串。
如果需要输入多行字符申,则使用QTextEdit
类。
定义输入掩码的字符,表4-5中列出了输入掩码的占位符和字面字符,并说明其如何控制数据输入。
掩码由掩码字符和分隔符字符串组成,后面可以跟一个分号和空白字符,空白字符在编辑后会从文本中删除的。掩码示例如表4-6所示。
EchoMode的显示效果
from PyQt5.QtWidgets import QApplication, QLineEdit , QWidget , QFormLayout
import sys
class lineEditDemo(QWidget):
def __init__(self, parent=None):
super(lineEditDemo, self).__init__(parent)
self.setWindowTitle("QLineEdit例子")
flo = QFormLayout()
pNormalLineEdit = QLineEdit( )
pNoEchoLineEdit = QLineEdit()
pPasswordLineEdit = QLineEdit( )
pPasswordEchoOnEditLineEdit = QLineEdit( )
flo.addRow("Normal", pNormalLineEdit)
flo.addRow("NoEcho", pNoEchoLineEdit)
flo.addRow("Password", pPasswordLineEdit)
flo.addRow("PasswordEchoOnEdit", pPasswordEchoOnEditLineEdit)
pNormalLineEdit.setPlaceholderText("Normal")
pNoEchoLineEdit.setPlaceholderText("NoEcho")
pPasswordLineEdit.setPlaceholderText("Password")
pPasswordEchoOnEditLineEdit.setPlaceholderText("PasswordEchoOnEdit")
# 设置显示效果
pNormalLineEdit.setEchoMode(QLineEdit.Normal)
pNoEchoLineEdit.setEchoMode(QLineEdit.NoEcho)
pPasswordLineEdit.setEchoMode(QLineEdit.Password)
pPasswordEchoOnEditLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)
self.setLayout(flo)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = lineEditDemo()
win.show()
sys.exit(app.exec_())
输入掩码
from PyQt5.QtWidgets import QApplication, QLineEdit , QWidget , QFormLayout
import sys
class lineEditDemo(QWidget):
def __init__(self, parent=None):
super(lineEditDemo, self).__init__(parent)
self.setWindowTitle("QLineEdit的输入掩码例子")
flo = QFormLayout()
pIPLineEdit = QLineEdit()
pMACLineEdit = QLineEdit()
pDateLineEdit = QLineEdit()
pLicenseLineEdit = QLineEdit()
pIPLineEdit.setInputMask("000.000.000.000;_")
pMACLineEdit.setInputMask("HH:HH:HH:HH:HH:HH;_")
pDateLineEdit.setInputMask("0000-00-00")
pLicenseLineEdit.setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#")
flo.addRow("数字掩码", pIPLineEdit)
flo.addRow("Mac掩码", pMACLineEdit)
flo.addRow("日期掩码", pDateLineEdit)
flo.addRow("许可证掩码", pLicenseLineEdit)
#pIPLineEdit.setPlaceholderText("111")
#pMACLineEdit.setPlaceholderText("222")
#pLicenseLineEdit.setPlaceholderText("333")
#pLicenseLineEdit.setPlaceholderText("444")
self.setLayout(flo)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = lineEditDemo()
win.show()
sys.exit(app.exec_())
QLienEdit标签控件的综合示例
from PyQt5.QtWidgets import QApplication, QLineEdit , QWidget , QFormLayout
from PyQt5.QtGui import QIntValidator , QDoubleValidator , QFont
from PyQt5.QtCore import Qt
import sys
class lineEditDemo(QWidget):
def __init__(self, parent=None):
super(lineEditDemo, self).__init__(parent)
e1 = QLineEdit()
e1.setValidator( QIntValidator() )
e1.setMaxLength(4)
e1.setAlignment( Qt.AlignRight )
e1.setFont( QFont("Arial",20))
e2 = QLineEdit()
e2.setValidator( QDoubleValidator(0.99,99.99,2))
flo = QFormLayout()
flo.addRow("integer validator", e1)
flo.addRow("Double validator",e2)
e3 = QLineEdit()
e3.setInputMask('+99_9999_999999')
flo.addRow("Input Mask",e3)
e4 = QLineEdit()
e4.textChanged.connect( self.textchanged )
flo.addRow("Text changed",e4)
e5 = QLineEdit()
e5.setEchoMode( QLineEdit.Password )
flo.addRow("Password",e5)
e6 = QLineEdit("Hello PyQt5")
e6.setReadOnly(True)
flo.addRow("Read Only",e6 )
e5.editingFinished.connect( self.enterPress )
self.setLayout(flo)
self.setWindowTitle("QLineEdit例子")
def textchanged(self, text):
print( "输入的内容为: "+text )
def enterPress( self ):
print( "已输入值" )
if __name__ == "__main__":
app = QApplication(sys.argv)
win = lineEditDemo()
win.show()
sys.exit(app.exec_())
第1个文本框e1,显示文本使用自定义字体、右对齐、允许输入整数。
第2个文本框e2,限制输入小数点后两位。
第3个文本框e3,需要一个输入掩码应用于电话号码。
第4个文本框e4,需要发射信号textChanged,连接到槽函数textchanged()。
第5个文本框e6,设置显示模式EchoMode为Password,需要发射editingfinished信号连接到槽函数enterPress(),一旦用户按下了回车键,该函数就会被执行。
第6个文本框e6,显示一个默认的文本,不能编辑,设置为只读的。
QTextEdit
QTextEdit类是一个多行文本框控件,可以显示多行文本内容,当文本内容超出控件显示范围时,可以显示水平个垂直滚动条。
QTextEdit不仅可以显示文本还可以显示HTML文档。
from PyQt5.QtWidgets import QApplication, QWidget , QTextEdit, QVBoxLayout , QPushButton
import sys
class TextEditDemo(QWidget):
def __init__(self, parent=None):
super(TextEditDemo, self).__init__(parent)
self.setWindowTitle("QTextEdit 例子")
self.resize(300, 270)
self.textEdit = QTextEdit( )
self.btnPress1 = QPushButton("显示文本")
self.btnPress2 = QPushButton("显示HTML")
layout = QVBoxLayout()
layout.addWidget(self.textEdit)
layout.addWidget(self.btnPress1)
layout.addWidget(self.btnPress2)
self.setLayout(layout)
self.btnPress1.clicked.connect(self.btnPress1_Clicked)
self.btnPress2.clicked.connect(self.btnPress2_Clicked)
def btnPress1_Clicked(self):
self.textEdit.setPlainText("Hello PyQt5!\n点击按钮")
def btnPress2_Clicked(self):
self.textEdit.setHtml("<font color='red' size='6'><red>Hello PyQt5!\n点击按钮。</font>")
if __name__ == "__main__":
app = QApplication(sys.argv)
win = TextEditDemo()
win.show()
sys.exit(app.exec_())
版权声明
本文为[左小田^O^]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45802081/article/details/124330018
边栏推荐
- Sea Level Anomaly 和 Sea Surface Height Anomaly 的区别
- MDN文档里面入参写法中括号‘[]‘的作用
- Transposed convolution
- C language - Spoof shutdown applet
- What is JSON? First acquaintance with JSON
- Radar equipment (greedy)
- Issue 36 summary of atcoder beginer contest 248
- The role of brackets' [] 'in the parameter writing method in MDN documents
- mysql-触发器、存储过程、存储函数
- MySQL的锁机制
猜你喜欢
Issue 36 summary of atcoder beginer contest 248
Fletter next generation graphics renderer impaller
JDBC连接数据库
深入源码分析Servlet第一个程序
MySQL的锁机制
filebrowser实现私有网盘
Establish excel bookkeeping book through setting context menu
JVM系列(3)——内存分配与回收策略
Transposed convolution
解决报错:ImportError: IProgress not found. Please update jupyter and ipywidgets
随机推荐
AcWing 1096. Detailed notes of Dungeon Master (3D BFS) code
关于二叉树的遍历
Fletter next generation graphics renderer impaller
2 - principes de conception de logiciels
Deconstruction function of ES6
JVM series (3) -- memory allocation and recycling strategy
MySQL query uses \ g, column to row
域内用户访问域外samba服务器用户名密码错误
Error 2003 (HY000) when Windows connects MySQL: can't connect to MySQL server on 'localhost' (10061)
delete和truncate
2.devops-sonar安装
redhat实现目录下特定文本类型内关键字查找及vim模式下关键字查找
Total score of [Huawei machine test] (how to deal with the wrong answer? Go back once to represent one wrong answer)
‘EddiesObservations‘ object has no attribute ‘filled‘
SQL statement simple optimization
Hotkeys, interface visualization configuration (interface interaction)
创建线程的三种方式
JS number capitalization method
线程的底部实现原理—静态代理模式
Differences between sea level anatomy and sea surface height anatomy