如何将 qtwidgets 中的 PasswordEdit 与窗口中的其他 QLineEdit 小部件一起使用
所以我想使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论