pythonpyqtpyqt5qgridlayoutcellspacing

PyQt5: gridlayout spacing issue


I am a newbie in PyQt5. I'm trying to create a cell grid with a button at the bottom:

 g = QGridLayout(window)     
 for i in range(N):
    for j in range(N):
        b = QPushButton()
        g.addWidget(b, i, j, 1, 1)
 g.addWidget(QPushButton("Last move"), N+4, N//2)

And this is what I get:

screenshot

As you see, the button spoils everything. How to can I get rid of the space between the cells?


Solution

  • You need to merge the cells together in the bottom row, so the button doesn't get a column of its own:

        button = QPushButton("Last move")
        g.addWidget(button, N, 0, 1, N, QtCore.Qt.AlignCenter)
    

    The third and fourth arguments set the row and column span of the cell. If you want the button to stretch the full width, omit the last argument.