LISHUZUOXUN_yangjiang/BaseFrontendManager/page_manager.py

198 lines
6.4 KiB
Python
Raw Normal View History

2024-09-23 14:54:15 +08:00
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QIcon
from LSZXPagesManagerLibrary.consensus import *
class PageSwitcher(QMainWindow):
"""
页面管理器通过向该管理器中注册界面在界面中实现跳转到其他界面并且传输数据
"""
def __init__(self):
super().__init__()
# 页面序号字典
self.page_dict = dict()
# 页面栈
self.tag_buttons = []
self.logo = self._init_logo_widget()
# 创建顶部按钮布局
self.top_layout = QHBoxLayout()
# 创建页面切换容器
self.stacked_widget = QStackedWidget()
self.wifi_widget = self.init_wifi_widget()
self.wifi_disconnect_widget = self.init_wifi_disconnect_widget()
self.wifi_stacked_widget = QStackedWidget()
self.wifi_stacked_widget.addWidget(self.wifi_widget)
self.wifi_stacked_widget.addWidget(self.wifi_disconnect_widget)
self.empty = QFrame()
self.wifi_layout = QVBoxLayout()
self.wifi_layout.addWidget(self.empty, 6)
self.wifi_layout.addWidget(self.wifi_stacked_widget, 12)
self.wifi_layout.addWidget(self.empty, 82)
self.center_layout = QHBoxLayout()
self.center_layout.addWidget(self.stacked_widget, 94)
self.center_layout.addLayout(self.wifi_layout, 6)
# 主布局
self.main_layout = QVBoxLayout()
self.main_layout.addLayout(self.top_layout)
self.main_layout.addLayout(self.center_layout)
# 创建一个中心部件,并设置布局
self.central_widget = QWidget()
self.central_widget.setLayout(self.main_layout)
self.central_widget.setStyleSheet(
"background-color: #ffffff;"
"border:none;"
)
# 向管理器中增加一个界面
# 添加页面page_widget、添加按钮page_name、把按钮和页面绑定关联点击按钮时切换页面
def add_page(self, page_widget, page_name):
self.stacked_widget.addWidget(page_widget)
tag_button = self.create_button(page_name)
self.tag_buttons.append(tag_button)
# 初始化时设置第一个按钮样式
first_button = self.tag_buttons[0]
self.change_button_style(first_button)
tag_button.mousePressEvent = lambda event, lbl=tag_button: self.clicked_reaction(event, lbl)
self.top_layout.addWidget(tag_button, 15)
self.top_layout.addWidget(self.logo, 10)
# 启动ui界面
def start_ui(self):
"""
启动UI
:return:
"""
self.setCentralWidget(self.central_widget)
self.showMaximized()
# self.showFullScreen() # 设置窗口全屏显示
# 按钮点击事件绑定切换后的页面
def clicked_reaction(self, event, label):
index = self.tag_buttons.index(label)
self.stacked_widget.setCurrentIndex(index)
for button in self.tag_buttons:
self.button_style(button)
self.change_button_style(label)
def handleCurrentChanged(self, index):
if index == 4: # 页面 2 的索引为 1
for button in self.tag_buttons:
button.hide()
else:
for button in self.tag_buttons:
button.show()
# 顶部按钮
def create_button(self, text):
button = QLabel(text)
button.setAlignment(Qt.AlignCenter)
self.button_style(button)
return button
def button_style(self, button):
button.setStyleSheet(
"font: 600 30px \"Microsoft YaHei UI\";"
"letter-spacing: 4px;"
"padding-top: 10px;"
"padding-bottom: 10px;"
"margin-left: 6px;"
"margin-right: 6px;"
)
def change_button_style(self, button):
button.setStyleSheet(
"font: 600 30px \"Microsoft YaHei UI\";"
"letter-spacing: 4px;"
"padding-top: 10px;"
"padding-bottom: 10px;"
"margin-left: 6px;"
"margin-right: 6px;"
"color: #409eff;"
)
def jump2(self, page_name, data):
"""
用于其他页面中请求跳转到其他界面
:param page_name: 目标跳转界面名称
:param data: 附带的页面数据
:return:
"""
page = self.page_dict[page_name]
# 更新页面数据
page.set_data(data)
page.refresh()
# 执行页面跳转
self.stacked_widget.setCurrentWidget(page)
def back2page(self, page_name):
pre_page = self.page_dict[page_name]
# 执行页面跳转
self.stacked_widget.setCurrentWidget(pre_page)
@staticmethod
def _init_logo_widget():
logo = QLabel()
logo.setStyleSheet(
"border-image: url(assets/horizontal_logo.png);"
"background-size: contain;"
"background-repeat: no-repeat;"
"background-position: center;"
"padding-top: 10px;"
"padding-bottom: 10px;"
)
logo.setAlignment(Qt.AlignCenter)
logo.setScaledContents(True)
return logo
@staticmethod
def init_wifi_widget():
wifi_frame = QPushButton()
icon = QIcon("assets/wifi.png")
wifi_frame.setIconSize(QSize(70, 70))
wifi_frame.setIcon(icon)
wifi_frame.setStyleSheet(
"background-color: qlineargradient("
"spread: pad,"
"x1: 0,"
"y1: 0,"
"x2: 1,"
"y2: 1,"
"stop: 0 rgba(113, 183, 255, 255),"
"stop: 1 rgba(79, 149, 224, 255),"
"stop: 2 rgba(56, 147, 242, 255));"
"border-style: none;"
"border-radius: 50%;"
"height: 100px;"
)
return wifi_frame
@staticmethod
def init_wifi_disconnect_widget():
wifi_disconnect_widget = QPushButton()
icon = QIcon("assets/wifi.png")
wifi_disconnect_widget.setIconSize(QSize(70, 70))
wifi_disconnect_widget.setIcon(icon)
wifi_disconnect_widget.setStyleSheet(
"background-color: #c2c2c3;"
"border-style: none;"
"border-radius: 50%;"
"height: 100px;"
)
return wifi_disconnect_widget
APP = QApplication(sys.argv)
BASE_PAGE_MANAGER = PageSwitcher() # 管理端页面管理器
def keep_ui():
sys.exit(APP.exec_())