A few days ago, I asked the same question that I have below but I wrote it out wrong and had an incorrect MRE (minimal reproducible example). I'm reposting (hopefully having fixed the concerns with the other post).
The problem: I can't get my QScrollArea to work. When I create one I (a) don't get a scrollbar and (b) the items inside keep getting smaller the more I add (as opposed to staying the same size)
The MRE (let me know if this is still incorrect for an MRE):
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QPushButton
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QScrollArea
class MainScreen(QWidget):
def __init__(self, parent=None):
# Initialize the class
super().__init__(parent)
# Create a box to hold everything in the window
self.window_layout = QVBoxLayout()
# Create a label
title = QLabel("Welcome!")
# Add the label to the window layout
self.window_layout.addWidget(title)
# Create the outer QVBoxLayout
self.outer_QVBoxLayout = QVBoxLayout()
# Create a QScrollArea
self.scroll = QScrollArea()
self.scroll.setWidgetResizable(True)
self.scroll.setFixedHeight(200)
self.scroll.setLayout(self.outer_QVBoxLayout)
# Add the QAScrollArea to the window
self.window_layout.addWidget(self.scroll)
# Enter Button to add the inner_QVBoxLayouts
button = QPushButton("button")
button.clicked.connect(self.button_clicked)
self.window_layout.addWidget(button)
# Add vertical layout box to the window
self.setLayout(self.window_layout)
def button_clicked(self):
# Make the entry layout
an_inner_QVBoxLayout = QVBoxLayout()
# Create a label to put into the layout
some_text = QLabel("Hello!")
an_inner_QVBoxLayout.addWidget(some_text)
self.outer_QVBoxLayout.addLayout(an_inner_QVBoxLayout)
if __name__ == '__main__':
# Create an instance of QApplication
app = QApplication(sys.argv)
# Create login screen
window = MainScreen()
# Show the window
window.show()
# Exit
sys.exit(app.exec_())
Thanks