N
N
nvlveu2020-11-09 11:56:56
PyQt
nvlveu, 2020-11-09 11:56:56

How to arrange widgets on top in QGridLayout?

Hello.
How to stack widgets on top in QGridLayout similar to tkinter's .pack method?

tkinter:
5fa9034d20d0a563141098.png

How it works for me in PyQt5:
5fa903f99fa64872653976.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bbkmzzzz, 2020-11-09
@nvlveu

Add QSpacerItem to the very bottom

import sys
from PySide2.QtWidgets import QApplication, QMainWindow, QGridLayout, QPushButton, QWidget, QSpacerItem, QSizePolicy


class Main(QMainWindow):
    def __init__(self):
        super(Main, self).__init__()
        centralWidget = QWidget(self)
        self.setCentralWidget(centralWidget)
        
        # добавляем компоновщик-сетку
        self.v_grid = QGridLayout(centralWidget)

        # добавляем кнопки
        for i in range(5):
            self.v_grid.addWidget(QPushButton(f"Button {i}"))
        
        # Добавляем QSpacer, политики размера нужны, так как по умолчанию они Fixed
        self.v_grid.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Main()
    ex.show()
    sys.exit(app.exec_())

In general, poke Qt Designer, it goes in the supply of Qt wrappers
here to look for Pyside2: $PyInterpreterDirectory$\Lib\site-packages\PySide2\designer.exe
It will give you the opportunity to look at widgets, and throw in simple forms \ parts of forms. How to use it all later, you can search here, on the toaster

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question