如何在 QLineEdit 中隐藏密码

How to hide password in QLineEdit

想隐藏“*”输入的密码。但是密码显示为原始文本...

class Form(QDialog):
    def __init__(self, parent = None):
        super(Form,self).__init__(parent)

        self.usernamelabel = QLabel("Username : ")
        self.passwordlabel = QLabel("Password : ")
        self.username = QLineEdit()
        self.password = QLineEdit()
        self.okbutton = QPushButton("Login")
        self.username.setPlaceholderText("Enter Username Here")
        self.password.setPlaceholderText("Enter Password Here")

        layout = QGridLayout()
        layout.addWidget(self.usernamelabel,0,0)
        layout.addWidget(self.passwordlabel,1,0)
        layout.addWidget(self.username,0,1)
        layout.addWidget(self.password,1,1)
        layout.addWidget(self.okbutton)
        self.setLayout(layout)

QLineEdit class 有 several modes 允许您控制其文本的显示方式。要仅显示星号 (*),请执行以下操作:

self.password = QLineEdit()
self.password.setEchoMode(QLineEdit.Password)
...
output = self.password.text()

PS:

要设置不同的密码字符,您可以使用此样式表 属性:

self.password.setStyleSheet('lineedit-password-character: 9679')

该数字是一个 unicode 代码点,在本例中为黑色圆圈 ()。