class MyGridLayout(QGridLayout):
def __init__(self, size: int):
# 初始化一个 n x n 的彩色块
super().__init__()
for i in range(0, size):
for j in range(0, size):
color_name = random.choice(QColor.colorNames())
self.addWidget(ColorWidget(QColor(color_name), '[{},{}]'.format(i, j)), i, j)
完整的示例代码
代码语言:python代码运行次数:0复制
import random
from PySide6.QtCore import Qt
from PySide6.QtGui import QColor, QFont, QPalette
from PySide6.QtWidgets import QApplication, QGridLayout, QHBoxLayout, QLabel, QLineEdit, QMainWindow, QPushButton, 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 MyGridLayout(QGridLayout):
def __init__(self, size: int):
# 初始化一个 n x n 的彩色块
super().__init__()
for i in range(0, size):
for j in range(0, size):
color_name = random.choice(QColor.colorNames())
self.addWidget(ColorWidget(QColor(color_name), '[{},{}]'.format(i, j)), i, j)
class MyColorfulGrid(QMainWindow):
def __init__(self):
super().__init__()
self.default_size = 3
self.new_size = self.default_size
self.grid_widget = MyGridLayout(self.default_size)
# 输入矩阵大小
self.size_input = QLineEdit(f'{self.default_size}')
self.size_input.setPlaceholderText(f'NxN 彩色矩阵方块,N 默认为 {self.default_size}')
self.size_input.returnPressed.connect(self.replace_grid_layout)
# 点击重新生成,这里可能不会改变矩阵大小
self.button = QPushButton('重新生成彩色矩阵方块')
self.button.clicked.connect(self.replace_grid_layout)
# 输入框和按钮需要水平放置
self.h_layout = QHBoxLayout() # 水平布局
self.h_layout.addWidget(self.size_input)
self.h_layout.addWidget(self.button)
# 网格需要垂直放置
self.v_layout = QVBoxLayout() # 垂直布局
self.v_layout.addLayout(self.h_layout)
self.v_layout.addLayout(self.grid_widget)
# 整个窗口需要展示输入框、按钮和网格
container = QWidget()
container.setLayout(self.v_layout)
self.setCentralWidget(container)
def input_size(self):
if not str.isdigit(self.size_input.text()):
self.new_size = self.default_size
else:
self.new_size = int(self.size_input.text())
self.button.setEnabled(False)
self.size_input.setEnabled(False)
self.replace_grid_layout()
self.button.setEnabled(True)
self.size_input.setEnabled(True)
def replace_grid_layout(self):
if not str.isdigit(self.size_input.text()):
self.new_size = self.default_size
else:
self.new_size = int(self.size_input.text())
tmp = MyGridLayout(self.new_size)
# 删除旧布局中的所有控件并销毁它们
for i in reversed(range(self.grid_widget.count())):
widget = self.grid_widget.itemAt(i).widget()
self.grid_widget.removeWidget(widget)
# 确保控件与旧的布局和窗口完全断开连接,从而避免可能的内存泄漏或其他问题
widget.setParent(None)
# 从布局中删除一个控件或布局时,它并不会自动销毁。您需要手动删除旧的布局中的控件并销毁它们
widget.deleteLater()
self.v_layout.removeItem(self.grid_widget)
self.grid_widget.setParent(None)
self.grid_widget.deleteLater()
self.v_layout.addLayout(tmp)
self.grid_widget = tmp
# update()方法用于请求重新绘制和刷新控件及其子控件
# 它通常在控件的外观或数据发生更改时使用,以便控件显示新的数据。
# 在许多情况下,Qt 会自动处理布局和控件的更新,因此不需要手动调用
self.update()
if __name__ == '__main__':
app = QApplication()
window = MyColorfulGrid()
window.show()
app.exec()