1

I have the following simple app setup. Presently the top half of the screen has 3 widgets representing where I will fill in my data later. The yellow section needs to be larger than the red and purple sections. How do I achieve this and how can I maintain the scale when resizing the window? I would like the red and purple sections to take up about 30% each and the yellow portion to take up the center 40%.

My App Layout

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

import sys
class Color(QWidget):

    def __init__(self, color):
        super(Color, self).__init__()
        self.setAutoFillBackground(True)
        self.color = color

        palette = self.palette()
        palette.setColor(QPalette.Window, QColor(self.color))
        self.setPalette(palette)

class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.setWindowTitle("My App")
        self.setMinimumWidth(600)
        self.setMinimumHeight(400)

        main_div = QVBoxLayout()
        main_div.setContentsMargins(0,0,0,0)
        main_div.setSpacing(0)

        layout2 = QHBoxLayout()
        layout2.addWidget(Color('red'))
        layout2.addWidget(Color('yellow'))
        layout2.addWidget(Color('purple'))
        main_div.addLayout( layout2 )
        main_div.addWidget(Color('blue'))

        self.widget = QWidget()
        self.widget.setLayout(main_div)
        self.setCentralWidget(self.widget)

    def resizeEvent(self,event):
        pass

app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec_()
Scott Rowley
  • 486
  • 1
  • 7
  • 30
  • 2
    `layout.addWidget` has an optional `stretch=x: int` argument. By setting different numbers, the widgets will take more or less space relative of each other. – mahkitah Aug 22 '23 at 20:06

1 Answers1

1

Per mahkitah I was able to change the needed lines here for the desired effect:

        layout2.addWidget(Color('red'), stretch=1)
        layout2.addWidget(Color('yellow'), stretch=2)
        layout2.addWidget(Color('purple'), stretch=1)
Scott Rowley
  • 486
  • 1
  • 7
  • 30