2017-06-26 32 views
0

首先,添加滚动区,这里是我的代码:PyQt5 - 无法在我的窗口

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) 

到目前为止,该代码根据我for函数的数量仅增加一个框架。我想添加一个滚动条,以便这些框架将显示在此栏区域内。所以,对于我在前两三次之后添加的每一帧,我希望它们在滚动条时显示出来。我已经尝试在这里搜索,但没有为我工作。也许我错过了一些东西,我真的不知道。

谢谢大家的期待。

回答

0

为了做到这一点,我认为你需要一个包含QScrollArea的“外部”框架或小部件,然后你可以将内部小部件添加到足够大的情况下允许滚动的内部小部件。我希望下面的例子足以让你开始实现这一点,无论你打算如何:

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 
` 
+0

非常感谢你!这有很大帮助。我相信这足以解决该滚动区域的任何问题! –