本文主要是介绍Pyside6实操笔记(一):系统页面跳转,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 背景
- 代码实现
背景
假设我们有个登录界面和注册界面,如果我们想要从登录界面跳转到注册界面注册用户名和密码,可以采取本篇博客的方式来实现。
代码实现
关键代码:
- 1.创建一个空的窗口
class shareInfo:mainwin = None
-
- 跳转代码
#编写跳转注册界面逻辑
shareInfo.mainwin = RegistWindow()
shareInfo.mainwin.show()
#同时关闭登录界面
self.close()
完整代码:
import sys, time
from PySide6.QtCore import Qt, QTranslator, QLocale
from PySide6.QtGui import QIcon, QPixmap
from PySide6.QtWidgets import QApplication
from qframelesswindow import FramelessWindow, StandardTitleBar, AcrylicWindow
from qfluentwidgets import setThemeColor, FluentTranslator, setTheme, Theme, SplitTitleBar
from my_ui.demo.login.Ui_LoginWindow import Ui_Form
from my_ui.demo.login.Ui_RegistWindow import Ui_Form as RUi_Form
from qfluentwidgets import InfoBar, InfoBarPosition
from main_entrence import main_sys_run, MainWindow
from utils.file_tools import get_user_info, write_user_info, get_passwordclass shareInfo:mainwin = Noneclass LoginWindow(AcrylicWindow, Ui_Form):def __init__(self):super().__init__()self.setupUi(self)# setTheme(Theme.DARK)self.xlsx_path = "files/user.xlsx"setThemeColor('#28afe9')self.setTitleBar(SplitTitleBar(self))self.titleBar.raise_()self.label.setScaledContents(False)self.setWindowTitle('自动标注系统登录界面')self.setWindowIcon(QIcon(":/images/logo.jpg"))self.resize(1000, 650)self.windowEffect.setMicaEffect(self.winId(), isDarkMode=False)self.titleBar.titleLabel.setStyleSheet("""QLabel{background: transparent;font: 13px 'Segoe UI';padding: 0 4px;color: white}""")desktop = QApplication.screens()[0].availableGeometry()w, h = desktop.width(), desktop.height()self.move(w//2 - self.width()//2, h//2 - self.height()//2)self.loginButton.clicked.connect(self.valid_user)self.findButton_.clicked.connect(self.regist_user)self.loginButton.setStyleSheet("QPushButton:hover { background-color: rgb(135,206,250); color: rgb(255,255,255)};")def regist_user(self):shareInfo.mainwin = RegistWindow()shareInfo.mainwin.show()self.close()def valid_user(self):df, USERS, PASSWORDS = get_user_info(self.xlsx_path)username = self.user_button.text()password = self.pw_button.text()if username in USERS:if str(password) == str(get_password(df, username)):InfoBar.success(title='恭喜登录成功',content="正在跳转系统界面!",orient=Qt.Horizontal,isClosable=True,position=InfoBarPosition.BOTTOM_RIGHT,duration=1000)shareInfo.mainwin = MainWindow()shareInfo.mainwin.show()self.close()else:InfoBar.error(title='用户名和密码验证失败!',content="",orient=Qt.Horizontal,isClosable=True,position=InfoBarPosition.BOTTOM_RIGHT,duration=-1)else:InfoBar.error(title='此用户未注册!',content="",orient=Qt.Horizontal,isClosable=True,position=InfoBarPosition.BOTTOM_RIGHT,duration=-1)def resizeEvent(self, e):super().resizeEvent(e)pixmap = QPixmap(":/images/background.jpg").scaled(self.label.size(), Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation)self.label.setPixmap(pixmap)class RegistWindow(AcrylicWindow, RUi_Form):def __init__(self):super().__init__()self.setupUi(self)# setTheme(Theme.DARK)self.xlsx_path = "files/user.xlsx"setThemeColor('#28afe9')self.setTitleBar(SplitTitleBar(self))self.titleBar.raise_()self.label.setScaledContents(False)self.setWindowTitle('自动标注系统注册界面')self.setWindowIcon(QIcon(":/images/logo.jpg"))self.resize(1000, 650)self.windowEffect.setMicaEffect(self.winId(), isDarkMode=False)self.titleBar.titleLabel.setStyleSheet("""QLabel{background: transparent;font: 13px 'Segoe UI';padding: 0 4px;color: white}""")desktop = QApplication.screens()[0].availableGeometry()w, h = desktop.width(), desktop.height()self.move(w//2 - self.width()//2, h//2 - self.height()//2)self.confirmButton.clicked.connect(self.valid_user)self.confirmButton.setStyleSheet("QPushButton:hover { background-color: rgb(135,206,250); color: rgb(255,255,255)};")def valid_user(self):USERS, PASSWORDS = get_user_info(self.xlsx_path)username = self.user_button.text()password = self.pw_button.text()c_password = self.pw1_button.text()print(username, password, c_password)#如果用户名为空,则显示报错信息if username == "":InfoBar.warning(title='提示',content="账号名不许为空!",orient=Qt.Horizontal,isClosable=True,position=InfoBarPosition.BOTTOM_RIGHT,duration=-1)else:#若用户名存在,报错if username in USERS:InfoBar.warning(title='提示',content="用户已经存在!",orient=Qt.Horizontal,isClosable=True,position=InfoBarPosition.BOTTOM_RIGHT,duration=-1)elif password != c_password:InfoBar.warning(title='提示',content="密码不一致,请重新输入!",orient=Qt.Horizontal,isClosable=True,position=InfoBarPosition.BOTTOM_RIGHT,duration=-1)else:write_user_info(self.xlsx_path, username, password)################################################shareInfo.mainwin = LoginWindow()shareInfo.mainwin.show()self.close()def resizeEvent(self, e):super().resizeEvent(e)# ":/images/background.jpg"pixmap = QPixmap(":/images/background.jpg").scaled(self.label.size(), Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation)self.label.setPixmap(pixmap)if __name__ == '__main__':app = QApplication(sys.argv)translator = FluentTranslator(QLocale())app.installTranslator(translator)w = LoginWindow()w.show()app.exec()
这篇关于Pyside6实操笔记(一):系统页面跳转的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!