Не работает echoMode() для QLineEdit в PyQt5
Функция echoMode()
не заменяет вводимый текст на звёздочки?
Код (проблема у объекта self.reg_input
):
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setFixedSize(700, 440)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setFocus()
self.centralWidget = QtWidgets.QWidget()
self.setCentralWidget(self.centralWidget)
self.fon = QtWidgets.QLabel(self)
self.fon.setPixmap(QtGui.QPixmap('images/fon.png'))
self.fon.resize(700, 440)
self.logo = QtWidgets.QLabel(self)
self.logo.setPixmap(QtGui.QPixmap('images/logo.png'))
self.logo.resize(46, 46)
self.logo.move(40, 30)
self.login_label = QtWidgets.QLabel(self)
self.login_label.setStyleSheet('''
QLabel {
border-image: url(images/text.png);
}
QLabel:hover {
border-image: url(images/light_text.png);
}
''')
self.login_label.resize(383, 42)
self.login_label.move(40, 120)
self.login_input = QtWidgets.QLineEdit(self)
# снятие фокуса с login_input
self.login_input.setFrame(False)
self.login_input.setStyleSheet('''
QLineEdit {
background-color: rgba(0, 0, 0, 0);
color: white;
}''')
self.login_input.resize(363, 15)
self.login_input.move(50, 135)
self.login_input.setFont(QtGui.QFont("Arial", 11))
self.reg_label = QtWidgets.QLabel(self)
self.reg_label.setStyleSheet('''
QLabel {
border-image: url(images/text.png);
}
QLabel:hover {
border-image: url(images/light_text.png);
}''')
self.reg_label.resize(383, 42)
self.reg_label.move(40, 196)
self.reg_input = QtWidgets.QLineEdit(self)
self.reg_input.setFrame(False)
self.reg_input.setStyleSheet('''
QLineEdit {
background-color: rgba(0, 0, 0, 0);
color: white;
}''')
self.reg_input.resize(363, 15)
self.reg_input.move(50, 211)
self.reg_input.setFont(QtGui.QFont("Arial", 11))
self.reg_input.echoMode() # не работает
self.exit_button = QtWidgets.QPushButton(self)
self.exit_button.setStyleSheet('''
QPushButton {
border-image: url(images/exit1.png);
}
QPushButton:hover {
border-image: url(images/exit2.png);
}''')
self.exit_button.resize(32, 32)
self.exit_button.move(668, 0)
self.exit_button.clicked.connect(exit)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Попробуйте так:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setFixedSize(700, 440)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setFocus()
self.centralWidget = QtWidgets.QWidget()
self.setCentralWidget(self.centralWidget)
self.fon = QtWidgets.QLabel(self)
self.fon.setPixmap(QtGui.QPixmap('images/fon.png'))
self.fon.resize(700, 440)
self.logo = QtWidgets.QLabel(self)
self.logo.setPixmap(QtGui.QPixmap('images/logo.png'))
self.logo.resize(46, 46)
self.logo.move(40, 30)
self.login_label = QtWidgets.QLabel(self)
self.login_label.setStyleSheet('''
QLabel {
border-image: url(images/text.png);
}
QLabel:hover {
border-image: url(images/light_text.png);
}''')
self.login_label.resize(383, 42)
self.login_label.move(40, 120)
# +++
self.login_input = QtWidgets.QLineEdit(
self, placeholderText=self.tr("Email or username"))
# снятие фокуса с login_input
self.login_input.setFrame(False)
self.login_input.setStyleSheet('''
QLineEdit {
background-color: rgba(0, 0, 0, 0);
color: blue; /* <---- white */
}''')
self.login_input.resize(363, 15)
self.login_input.move(50, 135)
self.login_input.setFont(QtGui.QFont("Arial", 11))
self.reg_label = QtWidgets.QLabel(self)
self.reg_label.setStyleSheet('''
QLabel {
border-image: url(images/text.png);
}
QLabel:hover {
border-image: url(images/light_text.png);
}''')
self.reg_label.resize(383, 42)
self.reg_label.move(40, 196)
# +++
self.reg_input = QtWidgets.QLineEdit(self,
placeholderText=self.tr("Password"),
echoMode=QtWidgets.QLineEdit.Password
)
self.reg_input.setFrame(False)
self.reg_input.setStyleSheet('''
QLineEdit {
background-color: rgba(0, 0, 0, 0);
color: blue; /* <---- white */
}''')
self.reg_input.resize(363, 15)
self.reg_input.move(50, 211)
self.reg_input.setFont(QtGui.QFont("Arial", 11))
# self.reg_input.echoMode() # не работает
self.exit_button = QtWidgets.QPushButton(self)
self.exit_button.setStyleSheet('''
QPushButton {
border-image: url(images/exit1.png);
}
QPushButton:hover {
border-image: url(images/exit2.png);
}''')
self.exit_button.resize(32, 32)
self.exit_button.move(668, 0)
self.exit_button.clicked.connect(exit)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())