2

So I want to make a form using Python PyQt5 that inputs the username and password.

This is what I have done so far

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)


        self.client_username_label = QtWidgets.QLabel(Dialog)
        self.client_username_label.setGeometry(QtCore.QRect(70, 120, 100, 30))
        self.client_username_label.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
        self.client_username_label.setObjectName("client_username_label")

        self.client_password_label = QtWidgets.QLabel(Dialog)
        self.client_password_label.setGeometry(QtCore.QRect(70, 170, 100, 30))
        self.client_password_label.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
        self.client_password_label.setObjectName("client_password_label")

        self.client_username_input = QtWidgets.QLineEdit(Dialog)
        self.client_username_input.setGeometry(QtCore.QRect(180, 110, 150, 30))
        self.client_username_input.setObjectName("client_username_input")

        self.client_password_input = QtWidgets.QLineEdit(Dialog)
        self.client_password_input.setGeometry(QtCore.QRect(180, 160, 150, 30))
        self.client_password_input.setObjectName("client_password_input")
        self.client_password_input.setEchoMode(QLineEdit.Password)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))

        self.client_username_label.setText(_translate("Dialog", "Client Username"))
        self.client_password_label.setText(_translate("Dialog", "Client Password"))

        self.client_username_input.setText(_translate("Dialog", "User Example"))
        self.client_password_input.setText(_translate("Dialog", "Password Example"))


def main_UI():
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main_UI()

This code will result this form


What I have to do now is I need to add a toggle button to show/hide passwords from this field. I found PasswordEdit() from this library.

Here is the example of using this class:

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_()

Unfortunately, the example shows the password field in full window.


I need to implement this PasswordEdit() class to my code. I tried several changes but still have not found the solution. Here is one of my implementation code, and it gives error

self.client_password_input = PasswordEdit(QtWidgets.QLineEdit(Dialog))
self.client_password_input.setGeometry(QtCore.QRect(180, 160, 150, 30))
self.client_password_input.setObjectName("client_password_input")
self.client_password_input.setEchoMode(QLineEdit.Password)
  • You need to use a QWidget as a container and set a proper [layout manager](//doc.qt.io/qt-5/layout.html) to which all child widgets will be added. – musicamante Mar 07 '22 at 19:02

0 Answers0