如何将 qtwidgets 中的 PasswordEdit 与窗口中的其他 QLineEdit 小部件一起使用

发布于 2025-01-12 11:19:05 字数 3378 浏览 0 评论 0原文

所以我想使用Python PyQt5制作一个表单来输入用户名和密码。

这就是我到目前为止所做的

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

此代码将生成此表单


我现在必须做的我需要添加一个切换按钮来显示/隐藏此字段中的密码。我从这个库中找到了 PasswordEdit()

以下是使用此类的示例:

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

不幸的是,该示例在 完整窗口 中显示密码字段。


我需要在我的代码中实现这个 PasswordEdit() 类。我尝试了几次更改,但仍然没有找到解决方案。 这是我的实现代码之一,它给出了错误

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文