python – How to make an input password (password field) in PyQt4?

Question:

To use a field similar to HTML input in Pyqt4, I usually use QtGui.QLineEdit .

But how can I make a field similar to the input password, which is a specific field for entering passwords?

Is there a way to define the "asterisks" that hide the text typed in QLineEdit itself, or is there a specific one?

Answer:

Use setEchoMode passing the enum QLineEdit.Password or QLineEdit.PasswordEchoOnEdit as a parameter

from PyQt4 import QtGui

#...

txtPw = QtGui.QLineEdit()
txtPw.setEchoMode(QtGui.QLineEdit.Password)
txtPw.show()
  • QLineEdit.Password Shows only "asterisks" when typing some text

  • QLineEdit.PasswordEchoOnEdit Shows the typed character and then changes it to an "asterisk"

See other echoMode enum values ​​here

Based on this answer

Scroll to Top