当前位置: 首页 > news >正文

PyQt5设计登录跳转界面

PyQt5登录跳转界面设计

import sys
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton, QWidget, \QFormLayout, QMessageBox, QSpacerItem, QSizePolicyclass LoginDialog(QDialog):  #登录界面dialogdialog_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) #跳转的界面1self.Page2=Page2Dialog(self) #跳转的界面2self.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_())

程序运行截图

程序运行截图

相关文章:

  • 使用net.sf.mpxj读取project的.mpp文件
  • 文件操作(2)(C语言版)
  • Tuple 元组
  • MAX30102驱动
  • 【ajax基础03】常用ajax请求方法和数据提交以及axios错误处理
  • Java23种设计模式(二)
  • MySQL系列-语法说明以及基本操作(二)
  • 力扣爆刷第153天之TOP100五连刷(相交、翻转、排序链表、螺旋矩阵、锯齿二叉树)
  • IPython 使用技巧整理
  • Linux系统之mtr命令的基本使用
  • 超多细节—app图标拖动排序实现详解
  • 简析:分账系统
  • 测试testing06182
  • 暑期计划打卡清单表怎么写 暑期待办计划清单
  • 干G货,性能测试基本方法和原则,
  • 【node学习】协程
  • 4个实用的微服务测试策略
  • httpie使用详解
  • Logstash 参考指南(目录)
  • PAT A1092
  • Perseus-BERT——业内性能极致优化的BERT训练方案
  • Phpstorm怎样批量删除空行?
  • Vue 动态创建 component
  • XML已死 ?
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 实战:基于Spring Boot快速开发RESTful风格API接口
  • 小程序button引导用户授权
  • 学习笔记TF060:图像语音结合,看图说话
  • 怎么把视频里的音乐提取出来
  • elasticsearch-head插件安装
  • ​Base64转换成图片,android studio build乱码,找不到okio.ByteString接腾讯人脸识别
  • ​第20课 在Android Native开发中加入新的C++类
  • # C++之functional库用法整理
  • # Kafka_深入探秘者(2):kafka 生产者
  • #include<初见C语言之指针(5)>
  • (3)选择元素——(14)接触DOM元素(Accessing DOM elements)
  • (9)STL算法之逆转旋转
  • (附源码)springboot高校宿舍交电费系统 毕业设计031552
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (三)docker:Dockerfile构建容器运行jar包
  • (一)【Jmeter】JDK及Jmeter的安装部署及简单配置
  • (原創) 如何優化ThinkPad X61開機速度? (NB) (ThinkPad) (X61) (OS) (Windows)
  • (转) SpringBoot:使用spring-boot-devtools进行热部署以及不生效的问题解决
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • (转)IOS中获取各种文件的目录路径的方法
  • .net Application的目录
  • .NET Core 将实体类转换为 SQL(ORM 映射)
  • .NET 跨平台图形库 SkiaSharp 基础应用
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • .NET:自动将请求参数绑定到ASPX、ASHX和MVC(菜鸟必看)
  • .net6+aspose.words导出word并转pdf
  • .NET实现之(自动更新)
  • @manytomany 保存后数据被删除_[Windows] 数据恢复软件RStudio v8.14.179675 便携特别版...
  • @ModelAttribute使用详解