简体   繁体   中英

PyQt5 - Can't add a scroll area in my window

First of all, here's my code:

class UpdateFrame(QtWidgets.QFrame):

    def __init__(self, parent=None):
        super().__init__(parent)

        self.setFixedSize(579, 450)
        self.setStyleSheet('background-color: white;'
                           'border: 1px solid #4f4f51;'
                           'border-radius: 5px;'
                           'margin: 5px;'
                           'padding: 5px;')

        self.setLayout(QtWidgets.QVBoxLayout())

        for i in range (5):
            listFrame = QtWidgets.QFrame()
            listFrame.setStyleSheet('backgrounf-color: white;'
                                    'border: 1px solid #4f4f51;'
                                    'border-radius: 0px;'
                                    'margin: 2px;'
                                    'padding: 2px')
            self.layout().addWidget(listFrame)

So far this code only add a Frame according to the number in my for function. I want to add a scroll bar so those frames will be displayed inside this bar area. So, for each frame that I add after the first two or three, I want them to show up while I roll the bar down. I already tryed to search here, but nothing works for me. Maybe I'm missing something, I really don't know.

Thank you all in anticipation.

In order to do this, I think you need to have an "outer" frame or widget that contains the QScrollArea, then you can add the inner widget to that which will be permitted to scroll if large enough. I hope the example below is enough to get you started on implementing this wherever you intend to:

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

class OuterFrame(QMainWindow):
    def __init__(self, parent=None):
        # Initialize the Main Window
        super(OuterFrame, self).__init__(parent)

        # Spec out the Outer Frame
        self.setFixedSize(579, 450)
        self.setStyleSheet('background-color: white;'
                           'border: 1px solid #4f4f51;'
                           'border-radius: 5px;'
                           'margin: 5px;'
                           'padding: 5px;')

        # Create a Scroll Area in this Frame
        self.scroll_area = QScrollArea()

        # Calling Our Frame Updater
        frameWidget = UpdateFrame(self)

        # Set the frame widget to be part of the scroll area
        self.scroll_area.setWidget(frameWidget)
        self.scroll_area.setWidgetResizable(True)
        self.layout().addWidget(self.scroll_area)

class UpdateFrame(QFrame):
    def __init__(self, parent=None):
        super(UpdateFrame, self).__init__(parent)

        layout = QVBoxLayout()
        self.setLayout(layout)

        for i in range(25):
            listFrame = QFrame()
            listFrame.setStyleSheet('background-color: white;'
                                    'border: 1px solid #4f4f51;'
                                    'border-radius: 0px;'
                                    'margin: 2px;'
                                    'padding: 2px')
            listFrame.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
            listFrame.setMinimumSize(QSize(50, 50))

            layout.addWidget(listFrame)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWindow = OuterFrame()
    mainWindow.show()
    sys.exit(app.exec_())  # only need one app, one running event loop
`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM