You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PySide6登录界面中,正确输入账号密码后添加按钮?

实现登录成功后显示推荐按钮的方案

你的代码目前混用了手动move布局和注释掉的布局管理器,导致新增控件的位置难以控制。建议启用布局管理器自动管理控件排列,再通过"初始隐藏、登录成功后显示"的逻辑实现需求,具体修改如下:

关键修改点

  • 启用QVBoxLayout布局管理器,删除手动move代码,让控件自动垂直排列
  • 提前创建推荐按钮并设置为隐藏状态
  • 在登录验证成功分支显示按钮,失败分支隐藏按钮(避免状态残留)

修改后的完整代码

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()
        # 启用主布局管理器
        self.setLayout(self.main_layout)

        font = QFont('Arial', 26)
        font.setBold(True)

        # 用户名输入区域
        container_user = QFrame(self)
        hbox_layout_user = QHBoxLayout(container_user)
        user_label = QLabel('USUARIO: ', container_user)
        self.user_input = QLineEdit(container_user)
        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.button_validation = QPushButton('Validar', self)
        self.button_validation.clicked.connect(self.Validar_Credenciales)

        # 结果提示标签
        self.resul_label = QLabel(self)

        # 创建推荐按钮,初始隐藏
        self.recommend_btn = QPushButton('查看视频推荐', self)
        self.recommend_btn.hide()
        # 可选:给按钮绑定点击事件
        # self.recommend_btn.clicked.connect(self.open_recommendations)

        # 将所有控件加入主布局
        self.main_layout.addWidget(container_user)
        self.main_layout.addWidget(container_password)
        self.main_layout.addWidget(self.button_validation)
        self.main_layout.addWidget(self.resul_label)
        self.main_layout.addWidget(self.recommend_btn)
        # 设置布局间距和边距,优化界面美观度
        self.main_layout.setSpacing(20)
        self.main_layout.setContentsMargins(50, 50, 50, 50)

    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()
            # 登录成功,显示推荐按钮
            self.recommend_btn.show()
        else:
            self.resul_label.setText("Ingresa bien tu contraseña estupido")
            self.resul_label.adjustSize()
            # 登录失败,隐藏推荐按钮
            self.recommend_btn.hide()

# 可选:按钮点击事件示例
#    def open_recommendations(self):
#        print("打开视频推荐页面")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    login = Login()
    sys.exit(app.exec())

说明

  1. 启用布局管理器后,所有控件会自动垂直排列,推荐按钮会默认放在结果标签下方,无需手动调整位置
  2. 推荐按钮初始通过hide()隐藏,仅在登录验证通过时调用show()显示
  3. 可根据需求给推荐按钮绑定自定义点击事件,实现跳转或其他业务逻辑

内容的提问来源于stack exchange,提问作者STE4LPH HYDR4

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 08:42:52