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