【发布时间】:2021-12-21 01:59:55
【问题描述】:
我在网格布局中创建了小部件。小部件基于窗口进行拉伸。是否可以避免拉伸并将它们对齐,如下图所示?我创建了一个代码来实现这一点,但我觉得这不是一个简单的解决方案。如果有更好的解决方案可以实现这一点,请分享。
网格布局结果:
from PyQt5.QtWidgets import *
app =QApplication([])
window=QWidget()
GL=QGridLayout(window)
GL.addWidget(QPushButton('R1C1'),0,0)
GL.addWidget(QPushButton('R1C2'),0,1)
GL.addWidget(QPushButton('R2C1'),1,0)
GL.addWidget(QPushButton('R1C1'),1,1)
window.showMaximized()
app.exec_()
要求的结果:
我的代码:
from PyQt5.QtWidgets import *
app =QApplication([])
window=QWidget()
VL=QVBoxLayout(window);HL=QHBoxLayout();VL.addLayout(HL)
GL=QGridLayout();HL.addLayout(GL)
GL.addWidget(QPushButton('R1C1'),0,0)
GL.addWidget(QPushButton('R1C2'),0,1)
GL.addWidget(QPushButton('R2C1'),1,0)
GL.addWidget(QPushButton('R1C1'),1,1)
HL.addStretch();VL.addStretch()
window.showMaximized()
app.exec_()
【问题讨论】:
-
看来你已经解决了,你的版本有什么问题(除了不必要的压缩和不可读的文字)?
标签: python layout pyqt5 qgridlayout