示例代码
代码语言:python
代码运行次数:0
复制import random
from PySide6.QtCore import Qt
from PySide6.QtGui import QColor, QFont, QPalette
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow, QTabWidget, QVBoxLayout, QWidget
class ColorWidget(QWidget):
def __init__(self, color: QColor, content: str):
super().__init__()
label = QLabel(content)
label.setAutoFillBackground(True)
palette = label.palette()
palette.setColor(QPalette.ColorGroup.Normal, QPalette.ColorRole.Window, color)
label.setPalette(palette)
label.setAlignment(Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignHCenter)
label.setFont(QFont('Arial', 40))
layout = QVBoxLayout()
layout.addWidget(label)
self.setLayout(layout)
class MyTabWidget(QMainWindow):
def __init__(self, tabs_num = 5):
super().__init__()
self.setWindowTitle('Tab Widget')
self.tab_widget = QTabWidget()
self.tab_widget.setTabPosition(QTabWidget.TabPosition.North)
self.tab_widget.setMovable(True)
# self.tab_widget.setDocumentMode(True)
# 获取原始的列表
color_names = QColor.colorNames()
# 随机洗牌打乱顺序
random.shuffle(color_names)
for i in range(0, tabs_num):
self.tab_widget.addTab(ColorWidget(QColor(color_names[i]), f'tab[{i}]'), color_names[i])
self.setCentralWidget(self.tab_widget)
if __name__ == '__main__':
app = QApplication()
ins = MyTabWidget(10)
ins.show()
app.exec()
运行效果
设置DocumentMode
运行效果