To help support developers in [[ countryRegion ]] I give a [[ localizedDiscount[couponCode] ]]% discount on all books and courses. Please share with friends and colleagues!

New to Qt5 and Python? Check out our complete PyQt5 tutorial.

PasswordEdit

Password editing field, with Show/Hide toggle

When building applications which require a password (or some other secret) from a user you should use fields that hide the input. This prevents shoulder surfing passers by from being able to read off the user's secret and gain access to accounts.

Qt QLinEdit fields can be toggled to hide input using the .setEchoMode method, passing in the constant QtWidgets.QLineEdit.Password to hide all input or QtWidgets.QLineEdit.Normal to reverse the display back to normal.

python
le = QtWidgets.QLineEdit()
le.setEchoMode(QtWidgets.QLineEdit.Password)

Sometimes however you may want to allow the user themselves to toggle the state of the display. This is commonly used for not so secret fields, such as WiFi passwords, or for situations where user-input is prone to error, for example on mobile or when passphrases are particularly long. Doing this gives the user the option to weigh up risk vs. usability themselves.

While you can achieve this with an additional button, it's become a convention from the web to add the show/hide toggle as an "eye" icon in the field itself. In this custom widget we've implemented this same behaviour by making use of Qt QLineEdit support for embedded actions with custom icons.

To use the widget install the qtwidgets Python library.

shell
pip3 install qtwidgets

You can then import the widget into your application using

python
from qtwidgets import PasswordEdit

There are other widgets available in the library too.

You can use the PasswordEdit widget as any other QLineEdit as it subclasses from the standard widget. This gives you access to all standard signals/methods.

You can test the widget out using the following demo code.

python
from PyQt5 import QtCore, QtGui, QtWidgets
from qtwidgets import PasswordEdit


class Window(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()

        password = PasswordEdit()
        self.setCentralWidget(password)


app = QtWidgets.QApplication([])
w = Window()
w.show()
app.exec_()
python
from PySide2 import QtCore, QtGui, QtWidgets
from qtwidgets import PasswordEdit


class Window(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()

        password = PasswordEdit()
        self.setCentralWidget(password)


app = QtWidgets.QApplication([])
w = Window()
w.show()
app.exec_()

Running the code you'll see a window appear containing a single line edit box, with an embedded eye icon. Try typing something in the box and then using the eye to toggle hide/show the content

Empty PasswordEdit field
Empty PasswordEdit field
Hidden PasswordEdit field
Hidden PasswordEdit field
Visible PasswordEdit field
Visible PasswordEdit field

You can opt to hide the visibility toggle if you wish, by passing show_visibility=False when creating the widget. In this case the widget behaves just like a QLineEdit except defaulting to hidden input.

python
pe = PasswordEdit(show_visibility=False)

The PasswordEdit widget is based on this original example by Kushal Das.

Enjoyed this?
I've also written a book.

Create Simple GUI Applications is my hands-on guide to making desktop apps with Python. Learn everything you need to build real apps.

Check it out