背景
假设我们有个登录界面和注册界面,如果我们想要从登录界面跳转到注册界面注册用户名和密码,可以采取本篇博客的方式来实现。
代码实现
关键代码:
- 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_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()