Issue
import sys
from PySide6.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QLineEdit, QVBoxLayout, QHBoxLayout, QFrame
from PySide6.QtGui import QFont
class Login(QWidget):
def __init__(self):
super().__init__()
self.start_ui()
def start_ui(self):
self.generar_formulario()
self.show()
def generar_formulario(self):
self.main_layout = QVBoxLayout()
container_user = QFrame(self)
hbox_layout_user = QHBoxLayout(container_user)
user_label = QLabel('USUARIO: ', container_user)
self.user_input = QLineEdit(container_user)
font = QFont('Arial', 26)
font.setBold(True)
user_label.setFont(font)
hbox_layout_user.addWidget(user_label)
hbox_layout_user.addWidget(self.user_input)
container_password = QFrame(self)
hbox_layout_password = QHBoxLayout(container_password)
password_label = QLabel('CONTRASEÑA: ', container_password)
self.password_input = QLineEdit(container_password)
self.password_input.setEchoMode(QLineEdit.Password)
password_label.setFont(font)
hbox_layout_password.addWidget(password_label)
hbox_layout_password.addWidget(self.password_input)
self.main_layout.addWidget(container_user)
self.main_layout.addWidget(container_password)
self.button_validation = QPushButton('Validar', self)
self.button_validation.clicked.connect(self.Validar_Credenciales)
self.main_layout.addWidget(self.button_validation)
self.resul_label = QLabel(self)
#self.resul_label.setFont(QFont('Arial', 12))
self.main_layout.addWidget(self.resul_label)
container_user.move(50, 50)
container_password.move(50, 150)
self.button_validation.move(150, 250)
self.resul_label.move(60, 350)
#self.setLayout(self.main_layout)
def Validar_Credenciales(self):
usuario = "Pepe"
password = "123"
usuario_get = self.user_input.text()
contraseña_get = self.password_input.text()
if usuario_get == usuario and contraseña_get == password:
self.resul_label.setText(f"\tBienvenido {usuario},\n aqui tenemos recomendaciones de videos")
self.resul_label.adjustSize()
else:
self.resul_label.setText("Ingresa bien tu contraseña estupido")
self.resul_label.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
login = Login()
sys.exit(app.exec())
I expected that when I entered everything was correct it would show me a button
Below the Label that says Welcome to the user
Solution
You can create the button but make it hidden
until the password and username matches the desired values by listening for the QLineEdit.textChanged
signal and checking the values entered into the two line edits every time the widgets contents are changed. Then you can call QPushButton.setHidden(False)
to show the button that was hidden.
Although I do not recommend storing user credentials in this manner... here is an example:
import sys
from PySide6.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QLineEdit, QVBoxLayout, QHBoxLayout, QFrame
from PySide6.QtGui import QFont
class Login(QWidget):
def __init__(self):
super().__init__()
self.start_ui()
def start_ui(self):
self.generar_formulario()
self.show()
def generar_formulario(self):
self.main_layout = QVBoxLayout()
container_user = QFrame(self)
hbox_layout_user = QHBoxLayout(container_user)
user_label = QLabel('USUARIO: ', container_user)
self.user_input = QLineEdit(container_user)
font = QFont('Arial', 26)
font.setBold(True)
user_label.setFont(font)
hbox_layout_user.addWidget(user_label)
hbox_layout_user.addWidget(self.user_input)
container_password = QFrame(self)
hbox_layout_password = QHBoxLayout(container_password)
password_label = QLabel('CONTRASEÑA: ', container_password)
self.password_input = QLineEdit(container_password)
self.password_input.setEchoMode(QLineEdit.Password)
password_label.setFont(font)
hbox_layout_password.addWidget(password_label)
hbox_layout_password.addWidget(self.password_input)
self.main_layout.addWidget(container_user)
self.main_layout.addWidget(container_password)
self.button_validation = QPushButton('Button', self)
self.button_validation.setHidden(True)
self.button_validation.clicked.connect(self.Validar_Credenciales)
self.main_layout.addWidget(self.button_validation)
self.resul_label = QLabel(self)
#self.resul_label.setFont(QFont('Arial', 12))
self.main_layout.addWidget(self.resul_label)
container_user.move(50, 50)
container_password.move(50, 150)
self.button_validation.move(150, 250)
self.resul_label.move(60, 350)
self.usuario = "Pepe"
self.password = "123"
self.password_input.textChanged.connect(self.on_text_changed)
self.user_input.textChanged.connect(self.on_text_changed)
#self.setLayout(self.main_layout)
def on_text_changed(self):
usuario_get = self.user_input.text()
contraseña_get = self.password_input.text()
if usuario_get == self.usuario and contraseña_get == self.password:
self.button_validation.setHidden(False)
def Validar_Credenciales(self):
usuario_get = self.user_input.text()
contraseña_get = self.password_input.text()
if usuario_get == self.usuario and contraseña_get == self.password:
self.resul_label.setText(f"\tBienvenido {self.usuario},\n aqui tenemos recomendaciones de videos")
self.resul_label.adjustSize()
else:
self.resul_label.setText("Ingresa bien tu contraseña estupido")
self.resul_label.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
login = Login()
sys.exit(app.exec())
Answered By - Alexander
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.