Search code examples
python-2.7passwordspysideqlineedit

How to hide password in QLineEdit


Want to hide password typed by "*". But the password is appearing as original text...

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)

Solution

  • The QLineEdit class has several modes which allow you to control how its text is displayed. To show only asterisks (*), do this:

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

    PS:

    To set a different password character, you can use this stylesheet property:

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

    The number is a unicode code point, which in this case is for a black circle ().