Pyside6实操笔记(一):系统页面跳转

文章目录

背景

假设我们有个登录界面和注册界面,如果我们想要从登录界面跳转到注册界面注册用户名和密码,可以采取本篇博客的方式来实现。

代码实现

关键代码

  • 1.创建一个空的窗口
class shareInfo:
    mainwin = None
    1. 跳转代码
#编写跳转注册界面逻辑
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_password

class shareInfo:
    mainwin = None

class 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()
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZY_dl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值