PyQt5菜单加多页签实现
pyqt tabs标签_哔哩哔哩_bilibili
代码实现
# coding:utf-8
import sys
from PyQt5.QtCore import Qt
from PyQt5 import QtCore,QtWidgets
from PyQt5.QtWidgets import QApplication,QWidget
from QhTabs01 import Ui_Form
from PyQt5.Qt import *
class QhTabsGUI(QWidget,Ui_Form):
"""
GUI交互部分代码
"""
def __init__(self,qh_parent = None):
self.QhFrameTabs = {}
# 初始化是否选中Tab标题字典
self.QhTabFrameStyleSheet = ["""
background-color: rgb(188, 218, 218);
border-top-left-radius: 8px; /* 通过设置左上角,右下角圆角得到特殊图形*/
border-top-right-radius: 8px;
border-bottom-left-radius: 8px;
border-bottom-right-radius: 8px;
border: 2px solid rgb(0, 120, 212);
""",
"""
background-color: rgb(235, 235, 235);
border-top-left-radius: 8px; /* 通过设置左上角,右下角圆角得到特殊图形*/
border-top-right-radius: 8px;
border-bottom-left-radius: 8px;
border-bottom-right-radius: 8px;
"""]
super().__init__(qh_parent)
# super(QhTabsGUI, self).__init__()
self.setupUi(self) # 调用Qh_SetupUI()函数,并把窗口作为实参传递给Qh_SetupUI()函
# 首页--将需要变化的控件(QLable控件)对象添加到列表中
self.QhFrameTabs[self.QhHomeBack_label.objectName()] = self.QhHomeBack_label # 添加到列表中,用于是否选中
self.QhHomeBut_pushButton.clicked.connect(lambda state,
Qhbutton=self.QhHomeBut_pushButton: self.QhMainButClicked(Qhbutton)) # 绑定首页按钮点击事件
self.QhTabWidget = self.QhTabsAdd_horizontalLayout # 页签承载栏
menu = self.createMenu(self.QhMenuBut_pushButton)
self.QhMenuBut_pushButton.setMenu(menu)
# 测试用
for i in range(50):
QhFraObj = self.QhAddTab(QhTabFu = self.scrollAreaWidgetContents,
QhTabName = "QhTab{}".format(i),
QhTabText = "阙辉你好-{}".format(i)) #控制在5个中文
self.QhTabWidget.addWidget(QhFraObj)
self.QhFrameTabs[QhFraObj.objectName()] = QhFraObj # 添加到列表中,用于是否选中
def QhAddTab(self,QhTabFu,QhTabName,QhTabText,QhTabIcon="url(:/test/QhImage/QhAiIoc.png);"):
"""
添加一个新的tab页面
:param QhTabFu: 父类对象
:param QhTabName: tab名称
:param QhTabText: tab显示文本
:return:
阙辉
"""
# self.scrollAreaWidgetContents
QhFraObjName ="{}_TabMain_Frame".format(QhTabName)
globals()["self.{}".format(QhFraObjName)] = QtWidgets.QFrame(QhTabFu)
QhFraObj = globals()["self.{}".format(QhFraObjName)]
QhFraObj.setMinimumSize(QtCore.QSize(138, 32))
QhFraObj.setMaximumSize(QtCore.QSize(138, 32))
QhFraObj.setStyleSheet("background-color: rgb(235, 235, 235);\n"
"border-top-left-radius: 8px; /* 通过设置左上角,右下角圆角得到特殊图形*/\n"
"border-top-right-radius: 8px; \n"
"border-bottom-left-radius: 8px;\n"
"border-bottom-right-radius: 8px;\n"
"\n"
"\n"
"")
QhFraObj.setFrameShape(QtWidgets.QFrame.StyledPanel)
QhFraObj.setFrameShadow(QtWidgets.QFrame.Raised)
QhFraObj.setObjectName(QhFraObjName)
QhIocLabObjName ="{}_Ioc_label".format(QhTabName)
globals()["self.{}".format(QhIocLabObjName)] = QtWidgets.QLabel(QhFraObj)
QhIocLabObj = globals()["self.{}".format(QhIocLabObjName)]
# self.QhIoc_label = QtWidgets.QLabel(self.QhTabMain_Frame)
QhIocLabObj.setGeometry(QtCore.QRect(2, 2, 28, 28))
QhIocLabObj.setMinimumSize(QtCore.QSize(28, 28))
QhIocLabObj.setMaximumSize(QtCore.QSize(28, 28))
QhIocLabObj.setStyleSheet("border-image: {}".format(QhTabIcon)) # 设置tab页面图标
QhIocLabObj.setText("")
QhIocLabObj.setObjectName(QhIocLabObjName)
QhTittleLabObjName ="{}_Tittle_label".format(QhTabName)
globals()["self.{}".format(QhTittleLabObjName)] = QtWidgets.QLabel(QhFraObj)
QhTittleLabObj = globals()["self.{}".format(QhTittleLabObjName)]
# self.QhTittle_label = QtWidgets.QLabel(self.QhTabMain_Frame)
QhTittleLabObj.setGeometry(QtCore.QRect(32, 2, 80, 28))
QhTittleLabObj.setMinimumSize(QtCore.QSize(80, 28))
QhTittleLabObj.setMaximumSize(QtCore.QSize(80, 28))
QhTittleLabObj.setStyleSheet("border: 0px;")
QhTittleLabObj.setText(QhTabText) # 设置tab页面标题
QhTittleLabObj.setObjectName(QhTittleLabObjName)
QhMainButObjName ="{}_MainBut_pushButton".format(QhTabName)
globals()["self.{}".format(QhMainButObjName)] = QtWidgets.QPushButton(QhFraObj)
QhMainButObj = globals()["self.{}".format(QhMainButObjName)]
# self.QhMainBut_pushButton = QtWidgets.QPushButton(self.QhTabMain_Frame)
QhMainButObj.setGeometry(QtCore.QRect(0, 0, 138, 32))
QhMainButObj.setMinimumSize(QtCore.QSize(138, 32))
QhMainButObj.setMaximumSize(QtCore.QSize(138, 32))
QhMainButObj.setStyleSheet("/*按钮图标样式 QH*/\n"
"QPushButton{\n"
" border: none; /*边框透明 QH*/\n"
" background: transparent; /*背景透明 QH*/ \n"
"} \n"
"\n"
"QPushButton:hover{ \n"
" border: 2px solid rgb(0, 120, 212);\n"
" border-top-left-radius: 8px; /* 通过设置左上角,右下角圆角得到特殊图形*/\n"
" border-top-right-radius: 8px; \n"
" border-bottom-left-radius: 8px;\n"
" border-bottom-right-radius: 8px;\n"
" \n"
"}\n"
"\n"
"QPushButton:pressed {\n"
" background-color:rgba(255, 255, 255, 188);\n"
" \n"
"}")
QhMainButObj.setText("")
QhMainButObj.setObjectName(QhMainButObjName)
QhMainButObj.clicked.connect(lambda state, Qhbutton=QhMainButObj: self.QhMainButClicked(Qhbutton)) # 绑定按钮点击事件
QhCloseIocObjName ="{}_CloseIoc_label".format(QhTabName)
globals()["self.{}".format(QhCloseIocObjName)] = QtWidgets.QLabel(QhFraObj)
QhCloseIocObj = globals()["self.{}".format(QhCloseIocObjName)]
# self.QhCloseIoc_label = QtWidgets.QLabel(self.QhTabMain_Frame)
QhCloseIocObj.setGeometry(QtCore.QRect(115, 6, 20, 20))
QhCloseIocObj.setMinimumSize(QtCore.QSize(20, 20))
QhCloseIocObj.setMaximumSize(QtCore.QSize(20, 20))
QhCloseIocObj.setStyleSheet("border-image: url(:/test/QhImage/QhClose.png);")
QhCloseIocObj.setText("")
QhCloseIocObj.setObjectName(QhCloseIocObjName)
QhCloseButObjName ="{}_CloseBut_pushButton".format(QhTabName)
globals()["self.{}".format(QhCloseButObjName)] = QtWidgets.QPushButton(QhFraObj)
QhCloseButObj = globals()["self.{}".format(QhCloseButObjName)]
# self.QhCloseBut_pushButton = QtWidgets.QPushButton(self.QhTabMain_Frame)
QhCloseButObj.setGeometry(QtCore.QRect(115, 6, 20, 20))
QhCloseButObj.setMinimumSize(QtCore.QSize(20, 20))
QhCloseButObj.setMaximumSize(QtCore.QSize(20, 20))
QhCloseButObj.setStyleSheet("/*按钮图标样式 QH*/\n"
"QPushButton{\n"
" border: none; /*边框透明 QH*/\n"
" background: transparent; /*背景透明 QH*/ \n"
"} \n"
"QPushButton:hover{ \n"
" border: 2px solid rgb(0, 0, 0);\n"
" background-color: rgba(255, 0, 0, 168);\n"
" border-top-left-radius: 4px; /* 通过设置左上角,右下角圆角得到特殊图形*/\n"
" border-top-right-radius: 4px; \n"
" border-bottom-left-radius: 4px;\n"
" border-bottom-right-radius: 4px;\n"
" \n"
"}\n"
"QPushButton:pressed {\n"
" background-color:rgba(255, 255, 255, 188);\n"
" \n"
"}")
QhCloseButObj.setText("")
QhCloseButObj.setObjectName(QhCloseButObjName)
QhCloseButObj.clicked.connect(lambda state, Qhbutton=QhCloseButObj: self.QhCloseButClicked(Qhbutton)) # 绑定按钮点击事件
return QhFraObj
def QhMainButClicked(self,Qhbutton):
"""
Tab按钮事件
阙辉
"""
QhButObjName = Qhbutton.objectName()
QhTabName = QhButObjName.split("_")[0]
print("按下的按钮对象名【{}】".format(QhButObjName))
QhFraObjName ="{}_TabMain_Frame".format(QhTabName) # 组合变化控件名称
self.QhTabsIsAtive(QhButObjName,QhFraObjName) # 判断是否选中,并改变样式
def QhTabsIsAtive(self,QhButObjName,QhFraObjName):
"""
是否选中tabs标题
:param QhButObjName: 按钮对象名称,用于判断是否是首页
:param QhFraObjName: 首页以为切换的对象名,即Frame对象
:return:
阙辉
"""
for QhKey,QhValue in self.QhFrameTabs.items():
if QhButObjName == "QhHomeBut_pushButton":
# 判断是否是首页
if QhKey == "QhHomeBack_label":
# 判断对象名是否为首页需要改变样式的对象名
QhValue.setStyleSheet(self.QhTabFrameStyleSheet[0])
else:
QhValue.setStyleSheet(self.QhTabFrameStyleSheet[1])
else:
if QhKey == QhFraObjName:
# 判断对象名不是首页需要改变样式的对象名
QhValue.setStyleSheet(self.QhTabFrameStyleSheet[0])
else:
QhValue.setStyleSheet(self.QhTabFrameStyleSheet[1])
def QhCloseButClicked(self,Qhbutton):
"""
Tab关闭按钮事件
阙辉
"""
QhButObjName = Qhbutton.objectName()
QhButObjName = Qhbutton.objectName()
print("按下的按钮对象名【{}】".format(QhButObjName))
QhTabName = QhButObjName.split("_")[0]
# 删除页签部分逻辑
self.QhDeleteTab(QhTabName)
def QhDeleteTab(self,QhTabName):
"""
删除页签
:param QhTabName: 页签名称
阙辉
"""
QhFraObjName ="{}_TabMain_Frame".format(QhTabName) # 组合变化控件名称
print("删除的页签【{}】,删除前数量({})".format(QhFraObjName,len(self.QhFrameTabs)))
QhFraObj = self.QhFrameTabs[QhFraObjName]
self.QhTabWidget.removeWidget(QhFraObj) # 从布局中删除页签
del self.QhFrameTabs[QhFraObjName] # 删除字典数据 对象
print("删除的页签【{}】,删除后数量({})".format(QhFraObjName,len(self.QhFrameTabs)))
def createMenu(self,part):
# 创建主菜单
main_menu = QMenu('主菜单', part)
# 创建主菜单 行为动作项目
main_action = QAction(QIcon('../images/png/edit.png'), '主菜单行为菜单项', main_menu)
# 主菜单行为动作 绑定数据
main_action.setData([1,2,3,4,5])
# 创建子菜单
sub_menu = QMenu('子菜单', main_menu)
# 创建子菜单 行为动作项目
sub_action1 = QAction('子菜单行为动作项目1', sub_menu)
sub_action2 = QAction('子菜单行为动作项目2', sub_menu)
sub_action3 = QAction('子菜单行为动作项目3', sub_menu)
sub_action4 = QAction('子菜单行为动作项目4', sub_menu)
# 子菜单行为动作 绑定数据
sub_action1.setData({"name":"Fz","age":36})
# 子菜单添加项目
sub_menu.addAction(sub_action1)
sub_menu.addAction(sub_action2)
sub_menu.addAction(sub_action3)
sub_menu.addAction(sub_action4)
# 主菜单添加项目
main_menu.addAction(main_action)
main_menu.addSeparator()
main_menu.addMenu(sub_menu)
return main_menu
if __name__ == '__main__':
# QApplication.setHighDpiScaleFactorRoundingPolicy(
# Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
# QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
# QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
qh_app = QApplication(sys.argv) # 创建应用实例 # 创建应用实例
qh_MyWindows = QhTabsGUI() # 创建窗口实例
qh_MyWindows.show() # 显示窗口
qh_n = qh_app.exec() # 执行exec()方法,进入事件循环,如果遇到窗口退出命令,返回整数qh_n
print(qh_n) # 输出输出关闭事件返回的整数
try: # 捕获程序退出事件
sys.exit(qh_n) # 通知python系统,结束程序运行
except SystemExit:
print("请在此做一些其他工作。") # python解释器停止执行前的工作