【问题标题】:Python PyQt5, How can I make QGridLayout rows close together and align to top? [duplicate]Python PyQt5,如何使 QGridLayout 行靠近并对齐到顶部? [复制]
【发布时间】:2022-01-26 23:33:24
【问题描述】:

我有一个 PyQt5 MainWindow,它只包含一个 QGridLayout。 QGridLayout 仅包含两行 QLabel 小部件,仅用于测试。 但是QLabel的两排并没有在一起而且很松散。如何使行靠近并在 MainWindow 顶部对齐 QGridLayout?代码在这里,谢谢!

class MainWindow(Ui_MainWindow, QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.gridLayout.addWidget(QLabel('First Row'), 0, 0)
        self.gridLayout.addWidget(QLabel('Second Row,oh! Ugly'), 1, 0)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

【问题讨论】:

  • 在布局底部添加扩展间隔:self.gridLayout.setRowStretch(self.gridLayout.rowCount(), 1)

标签: python pyqt5 qgridlayout


【解决方案1】:
    # => for align items
    self.gridLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
    # => row spacing
    self.gridLayout.setVerticalSpacing(5)

或者你可以使用

    self.gridLayout.setSpacing(5) # => both of vertical and horizontal
    self.gridLayout.setHorizontalSpacing(5) # => only horizontal-columns

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 2018-10-09
    相关资源
    最近更新 更多