2024-07-29 21:47:33
浏览数 (1)
QVBoxLayout的用法
代码语言:python
代码运行次数:0
复制class VerticallyLayout(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Vertically Layout')
v_layout = QVBoxLayout()
v_layout.addWidget(ColorWidget(QColor('red')))
v_layout.addWidget(ColorWidget(QColor('blue')))
v_layout.addWidget(ColorWidget(QColor('green')))
container = QWidget()
container.setLayout(v_layout)
self.setCentralWidget(container)
QHBoxLayout的用法
代码语言:python
代码运行次数:0
复制class HorizontallyLayout(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Horizontally Layout')
h_layout = QHBoxLayout()
h_layout.addWidget(ColorWidget(QColor('red')))
h_layout.addWidget(ColorWidget(QColor('blue')))
h_layout.addWidget(ColorWidget(QColor('green')))
container = QWidget()
container.setLayout(h_layout)
self.setCentralWidget(container)
完整示例代码
代码语言:python
代码运行次数:0
复制from PySide6.QtGui import QColor, QPalette
from PySide6.QtWidgets import QApplication, QHBoxLayout, QMainWindow, QVBoxLayout, QWidget
class ColorWidget(QWidget):
def __init__(self, color: QColor):
super().__init__()
self.setAutoFillBackground(True)
palette = self.palette()
palette.setColor(QPalette.ColorGroup.Normal, QPalette.ColorRole.Window, color)
self.setPalette(palette)
class VerticallyLayout(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Vertically Layout')
v_layout = QVBoxLayout()
v_layout.addWidget(ColorWidget(QColor('red')))
v_layout.addWidget(ColorWidget(QColor('blue')))
v_layout.addWidget(ColorWidget(QColor('green')))
container = QWidget()
container.setLayout(v_layout)
self.setCentralWidget(container)
class HorizontallyLayout(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Horizontally Layout')
h_layout = QHBoxLayout()
h_layout.addWidget(ColorWidget(QColor('red')))
h_layout.addWidget(ColorWidget(QColor('blue')))
h_layout.addWidget(ColorWidget(QColor('green')))
container = QWidget()
container.setLayout(h_layout)
self.setCentralWidget(container)
if __name__ == '__main__':
app = QApplication()
window = QWidget()
layout = QHBoxLayout()
layout.addWidget(VerticallyLayout())
layout.addWidget(HorizontallyLayout())
window.setLayout(layout)
window.show()
app.exec()
运行效果