2015-05-25

PyQt5 Password Dialog

The following is the complete source code showing how to program a password input dialog in PyQt5. The password dialog is based on QInputDialog, with the QLineEdit.Password set as the echo mode. Note that we do not need the QApplication to provide an event loop in this simple demonstration.
import sys

from PyQt5.QtWidgets import QApplication, QInputDialog, QLineEdit

def getPassword():
    text, ok = QInputDialog.getText(None, "Attention", "Password?", 
                                    QLineEdit.Password)
    if ok and text:
        print("password=%s" % text)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    getPassword()