PasswordEdit

Password editing field, with Show/Hide toggle
Heads up! You've already completed this tutorial.

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.

Create GUI Applications with Python & Qt5 by Martin Fitzpatrick — (PyQt5 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book

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.

Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

More info Get the book

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

PasswordEdit was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.