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

我以为当我输入的一切都是正确的时,它会显示一个按钮

在标签下面写着欢迎使用用户

推荐答案

您可以创建按钮,但将其设置为hidden,直到密码和用户名与所需的值匹配,方法是监听QLineEdit.textChanged信号,并在每次更改小部件内容时判断输入到两行编辑中的值.然后你可以拨打QPushButton.setHidden(False)来显示隐藏的按钮.

尽管我不建议以这种方式存储用户凭据...下面是一个例子:

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

Python-3.x相关问答推荐

为什么打印语句在Python多处理脚本中执行两次?

为什么 tkinter 在 tkinter 窗口外计算鼠标事件?

可以在 Python 的上下文管理器中调用 sys.exit() 吗?

以某种方式分割字符串

添加任意数量的 pandas 数据框

Python-Django 设置 Pandas DataFrame 的多索引不会分组/合并最后一个索引

如何将日期时间索引写入日期类型的表?

我应该如何调整我的变量,以便如果有任何单词符合其中的条件,程序会将其附加到新列表中?

使用 python 查找标记的元素

如何使用复选按钮更改 Pyplot 轴的属性?

如何使用 django rest 框架在 self forienkey 中删除多达 n 种类型的数据?

Python Regex 查找给定字符串是否遵循交替元音、辅音或辅音、元音的连续模式

包含值超出范围的 ID 的新 DataFrame 列?

获取以特定字母开头的姓氏

具有 2 个输入的 python 3 map/lambda 方法

使用打印时,用+连接是否比用,分隔更有效?

如何使用 d.items() 更改 for 循环中的所有字典键?

Python configparser 不会接受没有值的键

将 numpy.float64 列表快速转换为 Python 中的浮点数

Beautifulsoup 的单元测试失败