How to stretch QBoxLayout in PyQT5?

Viewed 5029

how to stretch the QHboxLayout and QVBoxLayout in PyQT5? Which method should I use? The pictures of result are given in links (as you see when window is small it also doesn't fit in the shape of the window)

from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, 
QLabel, QVBoxLayout, QHBoxLayout, QWidget, QDesktopWidget
import sys


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        label1 = QLabel('Введите слова')
        words = QTextEdit()

        label2 = QLabel('Результат')
        result = QTextEdit()

        vbox1 = QVBoxLayout()
        vbox1.addStretch(1)
        vbox1.addWidget(label1)
        vbox1.addWidget(words)

        vbox2 = QVBoxLayout()
        vbox2.addStretch(1)
        vbox2.addWidget(label2)
        vbox2.addWidget(result)

        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addLayout(vbox1)
        hbox.addLayout(vbox2)
        self.setLayout(hbox)


        self.setGeometry(300, 300, 500, 500)
        self.setWindowTitle('try')
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Window()
    sys.exit(app.exec_())

Small window

Full window

1 Answers

You have to put hbox to QWidget and set it as central widget.

main_widget = QWidget()
main_widget.setLayout(hbox)
self.setCentralWidget(main_widget)
Related