QLineEditEcho控件(如密码框)的4种显示方式:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QFont, QPalette, QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QToolTip, QHBoxLayout, QMainWindow, QLabel, QVBoxLayout, \
QDialog, QLineEdit, QGridLayout, QFormLayout
'''
QLineEdit控件与回显模式
基本功能:输入单行的文本
EchoMode 回显模式
4种回显模式
1 Normal
2 NoEcho
3 Password
4 PasswordEchoOnEdit
'''
class QLineEditEchoModel(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置定位和左上角坐标
self.setGeometry(300, 300, 400, 250)
# 设置窗口标题
self.setWindowTitle('QLineEditEcho控件(如密码框)的4种显示方式')
# 设置窗口图标
# self.setWindowIcon(QIcon('../web.ico'))
# 创建表单布局
formLayout = QFormLayout()
# 创建控件
normalLineEdit = QLineEdit()
noEchoLineEdit = QLineEdit()
passwordLineEdit = QLineEdit()
passwordEchoOnEditLineEdit = QLineEdit()
# 将控件添加到表单布局
formLayout.addRow("Normal",normalLineEdit)
formLayout.addRow("NoEcho",noEchoLineEdit)
formLayout.addRow("Password",passwordLineEdit)
formLayout.addRow("PasswordEchoOnEdit",passwordEchoOnEditLineEdit)
# 占位符
normalLineEdit.setPlaceholderText("Normal")
noEchoLineEdit.setPlaceholderText("NoEcho")
passwordLineEdit.setPlaceholderText("Password")
passwordEchoOnEditLineEdit.setPlaceholderText("PasswordEchoOnEdit")
# 设置控件的回显方式
# 正常
normalLineEdit.setEchoMode(QLineEdit.Normal)
# 不回显
noEchoLineEdit.setEchoMode(QLineEdit.NoEcho)
# 显示星号
passwordLineEdit.setEchoMode(QLineEdit.Password)
# 编辑时候正常回显,离开后显示星号
passwordEchoOnEditLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)
self.setLayout(formLayout)
if __name__ == '__main__':
app = QApplication(sys.argv)
# 设置应用图标
app.setWindowIcon(QIcon('../web.ico'))
w = QLineEditEchoModel()
w.show()
sys.exit(app.exec_())