创建的py文件和使用的模块,主要实现步骤都进行了标注
Login.py
import sys
import qdarkstyle
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt, QCoreApplication
from Login_more_level import Controller
qdarkstyle_sheet = qdarkstyle.load_stylesheet(qt_api='pyqt5')
if __name__ == '__main__':
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
app.setStyleSheet(qdarkstyle_sheet)
control_ = Controller() # 登录界面
control_.login_win_create() # 默认展示的是登录页面
sys.exit(app.exec_())
Login_more_level.py
使用的库
from PyQt5 import QtCore, QtWidgets
from Ui_LoginWindow import Ui_LoginWindow
from Ui_MyWindow import Ui_MyWindow
from Ui_ModelWindow import Ui_ModelWindow
初始化信息
class WinMessages: # 初始化信息
def __init__(self):
self.login_win = None # 一级
self.my_win = None # 二级
self.model_win = None # 三级
一级界面
class LoginWindow(QtWidgets.QMainWindow, Ui_LoginWindow): # 定义一级界面
sig1_2 = QtCore.pyqtSignal() # 跳转到二级界面的信号
sig1_3 = QtCore.pyqtSignal() # 跳转到二级界面的信号
def __init__(self):
super(LoginWindow, self).__init__()
self.setupUi(self)
# 跳转到每个模块的命令按钮
self.Button1_2.clicked.connect(self.get_sig1_2) # 到一级
self.Button1_2.clicked.connect(self.hide)
self.Button1_3.clicked.connect(self.get_sig1_3) # 到三级
self.Button1_3.clicked.connect(self.hide)
# 接收信号
def get_sig1_2(self):
self.sig1_2.emit()
# 接收信号
def get_sig1_3(self):
self.sig1_3.emit()
二级界面
class MyWindow(QtWidgets.QMainWindow, Ui_MyWindow): # 定义二级界面
sig2_3 = QtCore.pyqtSignal() # 跳转到三级界面的信号
sig2_1 = QtCore.pyqtSignal() # 跳转到一级界面的信号
def __init__(self):
super(MyWindow, self).__init__()
self.setupUi(self)
# 跳转到每个模块的菜单项
self.action2_3.triggered.connect(self.get_sig2_3) # 到三级
self.action2_1.triggered.connect(self.get_sig2_1) # 到一级
# 接收信号
def get_sig2_3(self):
self.sig2_3.emit()
# 接收信号
def get_sig2_1(self):
self.sig2_1.emit()
三级界面
class ModelWindow(QtWidgets.QMainWindow, Ui_ModelWindow): # 定义三级界面
def __init__(self):
super(ModelWindow, self).__init__()
self.setupUi(self)
控制器:控制界面跳转
class Controller(WinMessages): # 控制器:控制界面跳转
def __init__(self):
super(Controller, self).__init__()
'''一级界面'''
def login_win_create(self):
if not self.login_win:
self.login_win = LoginWindow() # 一级
# self.login_win.showMaximized()
self.login_win.sig1_2.connect(self.show_my_win_1)
self.login_win.sig1_3.connect(self.show_model_win_1)
self.login_win.show()
'''二级转一级界面'''
def show_login_win_2(self):
self.login_win.setGeometry(self.my_win.geometry()) # 一级取二级界面大小位置
self.login_win.show()
'''三级转一级界面'''
def show_login_win_3(self):
self.login_win.setGeometry(self.model_win.geometry()) # 一级取三级界面大小位置
self.login_win.show()
'''一级转二级界面'''
def show_my_win_1(self):
self.create_my_win()
self.my_win.setGeometry(self.login_win.geometry()) # 二级取一级界面大小位置
self.my_win.show()
'''三级转二级界面'''
def show_my_win_3(self):
self.create_my_win()
self.my_win.setGeometry(self.model_win.geometry()) # 二级取三级界面大小位置
self.my_win.show()
'''二级界面'''
def create_my_win(self):
if not self.my_win:
self.my_win = MyWindow() # 二级
# self.my_win.showMaximized()
self.my_win.sig2_3.connect(self.show_model_win_2) # 二级转三级
self.my_win.sig2_3.connect(self.my_win.hide) # 隐藏原页面
self.my_win.sig2_1.connect(self.show_login_win_2) # 二级转一级
self.my_win.sig2_1.connect(self.my_win.hide) # 隐藏原页面
'''一级转三级界面'''
def show_model_win_1(self):
self.model_win_create()
self.model_win.setGeometry(self.login_win.geometry()) # 三级取一级界面大小位置
self.model_win.show()
'''二级转三级界面'''
def show_model_win_2(self):
self.model_win_create()
self.model_win.setGeometry(self.my_win.geometry()) # 三级取二级界面大小位置
self.model_win.show()
'''三级界面'''
def model_win_create(self):
if not self.model_win:
self.model_win = ModelWindow() # 三级
# self.model_win.showMaximized() # 设置每个界面的默认最大化
self.model_win.Button3_2.clicked.connect(self.model_win.hide) # 隐藏原页面
self.model_win.Button3_2.clicked.connect(self.show_my_win_3) # 点击按钮三级转二级
self.model_win.Button3_1.clicked.connect(self.model_win.hide) # 隐藏原页面
self.model_win.Button3_1.clicked.connect(self.show_login_win_3) # 点击按钮三级转一级
解决闪动问题并优化代码
控制器类代码修改
class Controller(WinMessages): # 控制器:控制界面跳转
def __init__(self):
super(Controller, self).__init__()
'''一级界面'''
def login_win_create(self):
if not self.login_win:
self.login_win = LoginWindow() # 一级
self.login_win.sig1_2.connect(self.show_my_win_1)
self.login_win.sig1_3.connect(self.show_model_win_1)
self.login_win.show() # 正常初始界面
# self.login_win.showMaximized() # 最大化初始界面
self.my_win_create(self.login_win) # 创建二级界面
self.model_win_create(self.login_win) # 创建三级界面
'''二级转一级界面'''
def show_login_win_2(self):
'''self.my_win, self.is_login, self.login_win, self.g'''
self.win_update(self.my_win, self.login_win)
self.my_win.hide()
'''三级转一级界面'''
def show_login_win_3(self):
self.win_update(self.model_win, self.login_win)
self.model_win.hide()
'''一级转二级界面'''
def show_my_win_1(self, name):
# self.my_win_create()
self.win_update(self.login_win, self.my_win)
self.login_win.hide()
print(name)
'''三级转二级界面'''
def show_my_win_3(self, name):
# self.my_win_create()
self.win_update(self.model_win, self.my_win)
self.model_win.hide()
print(name)
'''二级界面'''
def my_win_create(self, login_win):
if not self.my_win:
self.my_win = MyWindow() # 二级
self.win_update(login_win, self.my_win)
self.my_win.sig2_3.connect(self.show_model_win_2) # 二级转三级
# self.my_win.sig2_3.connect(self.my_win.hide) # 隐藏原页面
self.my_win.sig2_1.connect(self.show_login_win_2) # 二级转一级
# self.my_win.sig2_1.connect(self.my_win.hide) # 隐藏原页面
self.my_win.hide()
'''一级转三级界面'''
def show_model_win_1(self):
# self.model_win_create()
self.win_update(self.login_win, self.model_win)
self.login_win.hide()
'''二级转三级界面'''
def show_model_win_2(self):
# self.model_win_create()
self.win_update(self.my_win, self.model_win)
self.my_win.hide()
'''三级界面'''
def model_win_create(self, login_win):
if not self.model_win:
self.model_win = ModelWindow() # 三级
self.win_update(login_win, self.model_win)
# self.model_win.Button3_2.clicked.connect(self.model_win.hide) # 隐藏原页面
self.model_win.Button3_2.clicked.connect(lambda: self.show_my_win_3("3_2")) # 点击按钮三级转二级
# self.model_win.Button3_1.clicked.connect(self.model_win.hide) # 隐藏原页面
self.model_win.Button3_1.clicked.connect(self.show_login_win_3) # 点击按钮三级转一级
self.model_win.hide()
def win_update(self, from_win, to_win): # 初始化界面,保证一致,界面跳转不闪动
if from_win.isMaximized():
to_win.showMaximized()
print("最大化")
else:
to_win.showNormal()
to_win.setGeometry(from_win.geometry()) # 取界面大小位置
print("同步")