我正在尝试使用
QLineEdit
在注册和登录表单中创建一个函数,以便在单击 QPushButton
时显示和隐藏密码。我是Python的初学者,我只是想这样做,但这非常困难...我的尝试不好,因为如果我单击眼睛按钮,密码就会显示,但如果再次单击隐藏它则不起作用.
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QPushButton, QLineEdit
import sys
import pymysql
pymysql.install_as_MySQLdb()
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, maxWidth=None):
super(MyWindow, self).__init__()
uic.loadUi('MainWindow.ui', self)
self.eyepass_show()
self.eyepass_hide()
self.btn_show_pwd.clicked.connect(self.eyepass_hide)
self.btn_show_pwd.clicked.connect(self.eyepass_show)
def eyepass_show(self):
self.line_password.setEchoMode(QLineEdit.Normal)
print('show pass')
def eyepass_hide(self):
self.line_password.setEchoMode(QLineEdit.Password)
print('hide pass')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
隐藏密码已显示,但如果再次单击隐藏则不起作用
您可以创建一个函数并切换可见性,而不是创建两个单独的方法(如
eyepass_show
和 eyepass_hide
)。另外,您尝试通过 self.btn_show_pwd.clicked.connect(self.eyepass_hide)
和 self.btn_show_pwd.clicked.connect(self.eyepass_show)
将同一信号两次连接到两种不同的方法
尝试这样的事情:
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QPushButton, QLineEdit
import sys
import pymysql
pymysql.install_as_MySQLdb()
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, maxWidth=None):
super(MyWindow, self).__init__()
uic.loadUi('MainWindow.ui', self)
self.eyepass_show()
self.eyepass_hide()
self.btn_show_pwd.clicked.connect(self.toggleVisibility)
def toggleVisibility(self):
if self.line_password.echoMode()==QLineEdit.Normal:
self.line_password.setEchoMode(QLineEdit.Password)
else:
self.line_password.setEchoMode(QLineEdit.Normal)
# self.btn_show_pwd.clicked.connect(self.eyepass_hide)
# self.btn_show_pwd.clicked.connect(self.eyepass_show)
#
# def eyepass_show(self):
# self.line_password.setEchoMode(QLineEdit.Normal)
# print('show pass')
#
# def eyepass_hide(self):
# self.line_password.setEchoMode(QLineEdit.Password)
# print('hide pass')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
另一种可能性是向 QLineEdit 添加可检查的 QAction 并连接到
toggled
(或 triggered
)信号。
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, maxWidth=None):
super(MyWindow, self).__init__()
uic.loadUi('MainWindow.ui', self)
icon = QtGui.QIcon('eye-icon.png')
self.showPassAction = QtWidgets.QAction(icon, 'Show password', self)
self.line_password.addAction(
self.showPassAction, QtWidgets.QLineEdit.TrailingPosition)
self.showPassAction.setCheckable(True)
self.showPassAction.toggled.connect(self.showPassword)
def showPassword(self, show):
self.line_password.setEchoMode(
QtWidgets.QLineEdit.Normal if show else QtWidgets.QLineEdit.Password)
如果您只想在按下鼠标时显示密码,则不要连接到
toggled
信号,而是找到该操作的子 QToolButton 并连接到 pressed
和 released
。在这种情况下,该操作不需要是可检查的。
self.line_password.addAction(
self.showPassAction, QtWidgets.QLineEdit.TrailingPosition)
showPassButton = self.line_password.findChild(QtWidgets.QAbstractButton)
showPassButton.pressed.connect(lambda: self.showPassword(True))
showPassButton.released.connect(lambda: self.showPassword(False))
如果您在多个地方需要相同的东西,最好创建一个具有这些功能的新 QWidget。
因此您在密码中使用“PasswordLineEdit”而不是“QLineEdit”:
from PySide6.QtWidgets import QLineEdit, QApplication, QMainWindow, QVBoxLayout, QWidget
from PySide6.QtGui import QIcon, QAction
class PasswordLineEdit(QLineEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.setEchoMode(QLineEdit.Password)
self.iconShow = QIcon('icons/eye.png')
self.iconHide = QIcon('icons/eye_blind.png')
self.showPassAction = QAction(self.iconShow, 'Show password', self)
self.addAction(self.showPassAction, QLineEdit.TrailingPosition)
self.showPassAction.setCheckable(True)
self.showPassAction.toggled.connect(self.togglePasswordVisibility)
def togglePasswordVisibility(self, show):
if show:
self.setEchoMode(QLineEdit.Normal)
self.showPassAction.setIcon(self.iconHide)
else:
self.setEchoMode(QLineEdit.Password)
self.showPassAction.setIcon(self.iconShow)
if __name__ == "__main__":
import sys
# ------ usage:
class MyWindow(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout(self)
self.line_password = PasswordLineEdit() # <-
layout.addWidget(self.line_password)
# ------
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec())
活动
优质内容区
您目前处于试用版,请按月订阅以享受对 Instagram 黑客面板的完全且无限制的访问(查看/编辑密码、发送/阅读消息...)
PyQt5 的一大优点是它会“自动”为您将信号连接到方法。如果您的应用程序中有一个名为 <mybutton>
的按钮和名为
on_<mybutton>_clicked
的方法,则 loadUi 将自动连接。所以模式是on_<widget_name>_<signal>
如果我们将此应用于您的问题,您应该将显示/隐藏按钮设置为切换按钮。在 Qt Designer 中,将 checkable 属性设置为 True (或执行
btn_show_pwd.clicked.setChecked(True)
)
代码:from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QPushButton, QLineEdit
import sys
#import pymysql
#pymysql.install_as_MySQLdb()
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, maxWidth=None):
super(MyWindow, self).__init__()
uic.loadUi('test1.ui', self)
self.line_password.setEchoMode(QLineEdit.Password)
def on_btn_show_pwd_toggled(self, checked):
if checked:
self.line_password.setEchoMode(QLineEdit.Password)
else:
self.line_password.setEchoMode(QLineEdit.Normal)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())