83 lines
2.7 KiB
Python
83 lines
2.7 KiB
Python
|
from PyQt5.QtWidgets import *
|
|||
|
from PyQt5.QtCore import Qt
|
|||
|
|
|||
|
|
|||
|
class PageSwitcher(QMainWindow):
|
|||
|
def __init__(self):
|
|||
|
super(PageSwitcher, self).__init__()
|
|||
|
self.tag_buttons = []
|
|||
|
# 创建顶部按钮布局
|
|||
|
# self.top_layout = QHBoxLayout()
|
|||
|
# 创建页面切换容器
|
|||
|
self.stacked_widget = QStackedWidget()
|
|||
|
self.setCentralWidget(self.stacked_widget)
|
|||
|
# 主布局
|
|||
|
main_layout = QVBoxLayout()
|
|||
|
# main_layout.addLayout(self.top_layout)
|
|||
|
main_layout.addWidget(self.stacked_widget)
|
|||
|
# 创建一个中心部件,并设置布局
|
|||
|
self.central_widget = QWidget()
|
|||
|
self.central_widget.setLayout(main_layout)
|
|||
|
# 设置主窗口的中心部件
|
|||
|
# self.setCentralWidget(central_widget)
|
|||
|
|
|||
|
# 添加页面(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)
|
|||
|
|
|||
|
# 启动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 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;"
|
|||
|
)
|
|||
|
|
|||
|
|