QScrollArea 没有附加到标签

QScrollArea does not attach to label

我正在尝试在 Box 布局中显示带有滚动条的图像标签。 但是,滚动区域以错误的大小出现在错误的位置。 你能告诉我我做错了什么吗?

import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton, QLabel, QScrollArea
from PyQt5.QtGui import QPixmap


class ApplicationWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        main_widget = QWidget(self)

        btn = QPushButton("Bye", self)
        btn.clicked.connect(self.close)

        img = QPixmap("1.jpg")
        label = QLabel(main_widget)
        label.setPixmap(img)

        scrollArea = QScrollArea(main_widget)
        scrollArea.setWidgetResizable(True) 
        scrollArea.setWidget(label)

        l = QVBoxLayout(main_widget)
        l.addWidget(label)
        l.addWidget(btn)

        self.setCentralWidget(main_widget)


    def closeEvent(self, ce):
        self.close()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    aw = ApplicationWindow()
    aw.show()
    app.exec_()

结果是:

问题是,您必须添加 QScrollArea,而不是将 QLabel 添加到 QVBoxLayout。您必须更改:

l.addWidget(label)

l.addWidget(scrollArea)