简体   繁体   English

PyQt5-无法在我的窗口中添加滚动区域

[英]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. 到目前为止,此代码仅根据我的for函数中的数字添加一个Frame。 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. 为此,我认为您需要一个包含QScrollArea的“外部”框架或小部件,然后可以将内部小部件添加到允许滚动的部件(如果足够大)。 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
`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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