PyQt5设计登录跳转界面

26 篇文章 0 订阅
20 篇文章 9 订阅

PyQt5登录跳转界面设计

import sys
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton, QWidget, \
    QFormLayout, QMessageBox, QSpacerItem, QSizePolicy
    
class LoginDialog(QDialog):  #登录界面dialog
    dialog_signal=pyqtSignal(bool)
    def __init__(self, parent=None):
        super(LoginDialog, self).__init__(parent)
        self.setWindowTitle('登录')
        self.initUI()
        self.resize(400, 400)
    def initUI(self):
        layout = QVBoxLayout()
        self.username_label = QLabel('用户名:')
        self.username_input = QLineEdit()
        self.vspacer = QSpacerItem(50, 50, QSizePolicy.Minimum, QSizePolicy.Expanding) #QSpacerItem 接受四个参数:宽度、高度、水平尺寸策略和垂直尺寸策略。QSizePolicy.Expanding 确保弹簧会尽可能地扩展。
        layout.addSpacerItem(self.vspacer)
        layout.addWidget(self.username_label)
        layout.addWidget(self.username_input)
        self.password_label = QLabel('密码:')
        self.password_input = QLineEdit()
        self.password_input.setEchoMode(QLineEdit.Password)
        layout.addWidget(self.password_label)
        layout.addWidget(self.password_input)
        layout.addSpacerItem(self.vspacer)
        self.login_button = QPushButton('登录')
        self.login_button.clicked.connect(self.on_login)
        layout.addWidget(self.login_button)
        layout.addSpacerItem(self.vspacer)
        self.setLayout(layout)
    def on_login(self):
        username = self.username_input.text()
        password = self.password_input.text()
        # 这里只是简单模拟,实际中你需要连接数据库或其他认证系统
        if username == 'admin' and password == '1':
            self.dialog_signal.emit(True)  # 如果登录成功,发送True信号
        else:
            self.dialog_signal.emit(False)  # 如果登录失败,发送False信号
            
class MainWindow(QMainWindow):  # 登录成功后显示的主界面
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle('主页面')
        self.Page1=Page1Dialog(self) #跳转的界面1
        self.Page2=Page2Dialog(self) #跳转的界面2
        self.initUI()
        self.resize(400, 400)
    def initUI(self):
        # 在这里添加主页面的UI元素
        self.page1_btn = QPushButton('界面1')
        self.page1_btn.clicked.connect(self.page1_btn_clicked)
        self.page2_btn = QPushButton('界面2')
        self.page2_btn.clicked.connect(self.page2_btn_clicked)
        self.vbox = QVBoxLayout()
        self.vbox.addWidget(self.page1_btn)
        self.vbox.addWidget(self.page2_btn)
        widGet = QWidget()
        widGet.setLayout(self.vbox)
        self.setCentralWidget(widGet)
    def page1_btn_clicked(self): #点击界面1按钮触发界面1显示
        self.Page1.show()
    def page2_btn_clicked(self): #点击界面2按钮触发界面2显示
        self.Page2.show()

class Page1Dialog(QDialog): #界面1设计
    def __init__(self, parent=None):
        super(QDialog, self).__init__(parent)
        self.setWindowTitle('界面1')
        self.initUI()
        self.resize(400, 400)
    def initUI(self):
        # 在这里添加主页面的UI元素
        self.line_input = QLineEdit()
        self.line_output = QLineEdit()
        self.label_input = QLabel('输入')
        self.line_input.textChanged.connect(self.line_output_getValue)
        self.label_output = QLabel('输出')
        gridlayout_input = QFormLayout()
        gridlayout_output = QFormLayout()
        gridlayout_input.addRow(self.label_input, self.line_input)
        gridlayout_output.addRow(self.label_output, self.line_output)
        self.vbox=QVBoxLayout()
        self.vbox.addLayout(gridlayout_input)
        self.vbox.addLayout(gridlayout_output)
        self.setLayout(self.vbox)
    def line_output_getValue(self):
        self.line_output.setText(self.line_input.text())
        
class Page2Dialog(QDialog): #界面2设计
    def __init__(self, parent=None):
        super(QDialog, self).__init__(parent)
        self.setWindowTitle('界面2')
        self.initUI()
        self.resize(400, 400)
    def initUI(self):
        # 在这里添加主页面的UI元素
        self.line_input = QLineEdit()
        self.line_input.textChanged.connect(self.line_output_getValue)
        self.line_output = QLineEdit()
        self.label_input = QLabel('输入')
        self.label_output = QLabel('输出')
        gridlayout_input = QFormLayout()
        gridlayout_output = QFormLayout()
        gridlayout_input.addRow(self.label_input, self.line_input)
        gridlayout_output.addRow(self.label_output, self.line_output)
        self.vbox=QVBoxLayout()
        self.vbox.addLayout(gridlayout_input)
        self.vbox.addLayout(gridlayout_output)
        self.setLayout(self.vbox)
    def line_output_getValue(self):
        self.line_output.setText(self.line_input.text())
        
class App(QApplication):
    def __init__(self, sys_argv):
        super(App, self).__init__(sys_argv)
        self.main_window = MainWindow()
        self.login_dialog = LoginDialog()
        self.login_dialog.dialog_signal.connect(self.on_login_finished)
        self.login_dialog.show()
    def on_login_finished(self, result): #点击登录后执行的函数,该函数与登录界面的dialog_signal信号绑定,接收来自登录界面点击登录按钮后发送的bool值信号
        if result:  # 如果登录成功(即接收到True信号)
            self.login_dialog.close() #关闭登录界面
            self.main_window.show() #显示登录后的主界面
        else:  # 如果登录失败(即接收到False信号)
            QMessageBox.critical(None, '错误提示', '账号或密码输入错误', QMessageBox.Ok) #弹出密码验证失败提示框

if __name__ == '__main__':
    app = App(sys.argv)
    sys.exit(app.exec_())

程序运行截图

程序运行截图

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值