1、准备要跳转到的界面
在QT设计师中可以设计一个简单的界面作为登录后要跳转的界面(后期会美化),例如下图所示:
同时编写界面逻辑代码main_win.py
,测试。
from PySide6.QtWidgets import QApplication,QMainWindow
from ui.Ui_mainwin import Ui_MainWindow
import sys
class MainWin(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
if __name__=='__main__':
app = QApplication(sys.argv)
window = MainWin()
sys.exit(app.exec())
2、编写跳转逻辑
找到登录函数中对应的登录成功部分代码,具体的登录和注册代码可以参考我的上一篇博客【PySide6】登录和注册信号绑定以及代码实现。
登录函数修改如下:
def signIn(self):
#获取用户名和密码
username = self.ui.lineEdit.text().strip()
password = self.ui.lineEdit_2.text().strip()
#获取已注册用户信息
USERS = get_user_info()
if username not in USERS.keys():
replay = QMessageBox.warning(self,'!','用户不存在',QMessageBox.Yes)
self.gotoStack(0)
else:
#判断密码是否正确
if USERS.get(username) == password:
#编写跳转逻辑
shareInfo.mainwin = MainWin()
shareInfo.mainwin.show()
#同时关闭登录界面
self.close()
else:
replay = QMessageBox.warning(self,'!','密码输入错误',QMessageBox.Yes)
self.gotoStack(0)
其中shareInfo为共享信息的类模块
class shareInfo:
mainwin = None
为了方便大家的使用我将完整登录代码放置如下:
from PySide6.QtWidgets import QApplication,QMainWindow,QWidget,QGridLayout,QFileDialog,QMessageBox
from PySide6.QtCore import Qt,QThread,Signal
from PySide6.QtCore import QPropertyAnimation, QEasingCurve, QEvent, QTimer,QTime
from PySide6.QtGui import QIcon,QPainter,QBrush,QColor,QCursor,QPixmap,QImage,QConicalGradient,QPen,QFont
from PySide6 import *
from PySide6.QtWidgets import *
import sys
from ui.Ui_login_win import Ui_MainWindow
from main_win import MainWin
from utils.utils import save_user_info,get_user_info
from utils.share import shareInfo
class LoginMainWin(QMainWindow):
def __init__(self) -> None:
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#隐藏窗口边框
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.show()
self.init_slot()
#初始化信号槽
def init_slot(self):
#初始显示界面标号0
self.gotoStack(0)
#登录界面按钮绑定界面0
self.ui.login_2.clicked.connect(lambda: self.gotoStack(0))
#注册界面按钮绑定界面1
self.ui.register_2.clicked.connect(lambda: self.gotoStack(1))
#取消按钮绑定界面0
self.ui.cancel.clicked.connect(lambda: self.gotoStack(0))
#绑定注册功能
self.ui.register_4.clicked.connect(self.new_account)
#绑定登录功能
self.ui.login.clicked.connect(self.signIn)
#隐藏输入密码
self.ui.lineEdit_2.setEchoMode(QLineEdit.Password)
def signIn(self):
#获取用户名和密码
username = self.ui.lineEdit.text().strip()
password = self.ui.lineEdit_2.text().strip()
#获取已注册用户信息
USERS = get_user_info()
if username not in USERS.keys():
replay = QMessageBox.warning(self,'!','用户不存在',QMessageBox.Yes)
self.gotoStack(0)
else:
#判断密码是否正确
if USERS.get(username) == password:
#编写跳转逻辑
shareInfo.mainwin = MainWin()
shareInfo.mainwin.show()
#同时关闭登录界面
self.close()
else:
replay = QMessageBox.warning(self,'!','密码输入错误',QMessageBox.Yes)
self.gotoStack(0)
def new_account(self):
#获取已存在用户
USERS = get_user_info()
#获取用户名和密码
new_username = self.ui.lineEdit_3.text().strip()
new_userpwd = self.ui.lineEdit_4.text().strip()
new_userpwd_2 = self.ui.lineEdit_5.text().strip()
#如果用户名为空,则显示报错信息
if new_username == "":
replay = QMessageBox.warning(self,'!','账号名不许为空',QMessageBox.Yes)
else:
#若用户名存在,报错
if new_username in USERS.keys():
replay = QMessageBox.warning(self,'!','账号已存在',QMessageBox.Yes)
self.gotoStack(1)
else:
#若密码不一致则报错
if new_userpwd != new_userpwd_2:
replay = QMessageBox.warning(self,'!','密码不一致',QMessageBox.Yes)
self.gotoStack(1)
else:
#注册成功
replay = QMessageBox.warning(self,'!','注册成功',QMessageBox.Yes)
#保存注册信息
save_user_info(new_username,new_userpwd)
#跳转登录界面
self.gotoStack(0)
#stackwidget 切换
def gotoStack(self, index: int):
self.ui.lineEdit.clear()
self.ui.lineEdit_2.clear()
self.ui.lineEdit_3.clear()
self.ui.lineEdit_4.clear()
self.ui.lineEdit_5.clear()
self.ui.stackedWidget.setCurrentIndex(index)
#窗口拖动
##############################################################################
def mousePressEvent(self,event):
if event.button() == Qt.LeftButton and self.isMaximized() == False:
self.mm_flag = True
self.m_Position = event.globalPos() - self.pos()
event.accept()
self.setCursor(QCursor(Qt.OpenHandCursor))
def mouseMoveEvent(self,mouseMove):
if Qt.LeftButton and self.mm_flag:
self.move(mouseMove.globalPos()-self.m_Position)
mouseMove.accept()
def mouseReleaseEvent(self,mouse_event):
self.mm_flag = False
self.setCursor(QCursor(Qt.ArrowCursor))
##############################################################################
if __name__ == '__main__':
app = QApplication(sys.argv)
#app.setWindowIcon(QIcon(".\images\icon.ico"))
window = LoginMainWin()
sys.exit(app.exec())
3、参考视频
具体的参考视频请关注我的B站@鲸可落