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.
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.
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.
pip3 install qtwidgets
You can then import the widget into your application using
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.
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_()
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
Hidden 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.
pe = PasswordEdit(show_visibility=False)
PasswordEdit widget is based on this original example by Kushal Das.
Create GUI Applications with Python & Qt (PySide6 Edition)
The easy way to create desktop applications
Create GUI Applications is the complete guide to building GUI applications with Python. Starting from the very basics of creating a desktop window this book takes you on a tour of the key features of PySide6 you can use to build real-life applications.
To support developers in [[ countryRegion ]] I give a [[ localizedDiscount[couponCode] ]]% discount on all books and courses.
[[ activeDiscount.description ]] I'm giving a [[ activeDiscount.discount ]]% discount on all books and courses.