[英]PyQt5 - Can't add a scroll area in my window
首先,這是我的代碼:
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
函數中的數字添加一個Frame。 我想添加一個滾動條,以便那些框架將顯示在此條區域內。 因此,對於我在前兩三個后添加的每個幀,我希望它們在滾動條時顯示出來。 我已經嘗試過在這里搜索,但對我沒有任何幫助。 也許我錯過了一些東西,我真的不知道。
謝謝大家的期待。
為此,我認為您需要一個包含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
`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.