0

Я в питоне новичек как и в PyQT5, извиняюсь если вопрос глупый: Как добавить множество виджетов (в моем случае картинок) в QScrollArea по возможности с QGridLayout как в коде:

import sys
from PyQt5 import QtWidgets, QtGui

class Window(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)

        self.imageColors = QtGui.QPixmap("colors.jfif").scaled(450 , 450)
        self.imageTreeNight = QtGui.QPixmap("treeNight.jpg").scaled(450 , 450)
        self.imageAbstract = QtGui.QPixmap("abstract.jfif").scaled(450 , 450)
        self.imageWoodWall = QtGui.QPixmap("woodWall.jfif").scaled(450 , 450)
        self.imageSign = QtGui.QPixmap("sign.jpg").scaled(450 , 450)
        self.imageLeaves = QtGui.QPixmap("leaves.jpg").scaled(450 , 450)

        self.labelColors = QtWidgets.QLabel()
        self.labelTreeNight = QtWidgets.QLabel()
        self.labelAbstract = QtWidgets.QLabel()
        self.labelWoodWall = QtWidgets.QLabel()
        self.labelSign = QtWidgets.QLabel()
        self.labelLeaves = QtWidgets.QLabel()

        self.labelColors.setPixmap(self.imageColors)
        self.labelTreeNight.setPixmap(self.imageTreeNight)
        self.labelAbstract.setPixmap(self.imageAbstract)
        self.labelWoodWall.setPixmap(self.imageWoodWall)
        self.labelSign.setPixmap(self.imageSign)
        self.labelLeaves.setPixmap(self.imageLeaves)

        self.layout = QtWidgets.QGridLayout()
        self.layout.addWidget(self.labelColors, 0 , 0)
        self.layout.addWidget(self.labelTreeNight, 0 , 1)
        self.layout.addWidget(self.labelAbstract, 1 , 0)
        self.layout.addWidget(self.labelWoodWall, 1 , 1)
        self.layout.addWidget(self.labelSign, 2 , 0)
        self.layout.addWidget(self.labelLeaves, 2 , 1)

        self.setLayout(self.layout)

if __name__ == "__main__":
    application = QtWidgets.QApplication([])

    window = Window()
    window.setWindowTitle("Window")
    window.setFixedSize(900, 900)
    window.show()

    sys.exit(application.exec_())

1 ответ 1

2

Нужно ту сетку с виджетами-картинками поместить на виджет, а его поместить в QScrollArea через метод QScrollArea:setWidget.

Пример:

import sys
from PyQt5 import QtWidgets, QtGui


class Window(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.imageColors = QtGui.QPixmap("colors.jfif").scaled(450 , 450)
        self.imageTreeNight = QtGui.QPixmap("treeNight.jpg").scaled(450 , 450)
        self.imageAbstract = QtGui.QPixmap("abstract.jfif").scaled(450 , 450)
        self.imageWoodWall = QtGui.QPixmap("woodWall.jfif").scaled(450 , 450)
        self.imageSign = QtGui.QPixmap("sign.jpg").scaled(450 , 450)
        self.imageLeaves = QtGui.QPixmap("leaves.jpg").scaled(450 , 450)

        self.labelColors = QtWidgets.QLabel()
        self.labelTreeNight = QtWidgets.QLabel()
        self.labelAbstract = QtWidgets.QLabel()
        self.labelWoodWall = QtWidgets.QLabel()
        self.labelSign = QtWidgets.QLabel()
        self.labelLeaves = QtWidgets.QLabel()

        self.labelColors.setPixmap(self.imageColors)
        self.labelTreeNight.setPixmap(self.imageTreeNight)
        self.labelAbstract.setPixmap(self.imageAbstract)
        self.labelWoodWall.setPixmap(self.imageWoodWall)
        self.labelSign.setPixmap(self.imageSign)
        self.labelLeaves.setPixmap(self.imageLeaves)

        # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        layout = QtWidgets.QGridLayout()
        layout.addWidget(self.labelColors, 0 , 0)
        layout.addWidget(self.labelTreeNight, 0 , 1)
        layout.addWidget(self.labelAbstract, 1 , 0)
        layout.addWidget(self.labelWoodWall, 1 , 1)
        layout.addWidget(self.labelSign, 2 , 0)
        layout.addWidget(self.labelLeaves, 2 , 1)

        scroll_widget = QtWidgets.QWidget()
        scroll_widget.setLayout(layout)

        scroll_area = QtWidgets.QScrollArea()
        scroll_area.setWidget(scroll_widget)
        scroll_area.setWidgetResizable(True)

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(scroll_area)
        # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        self.setLayout(self.layout)


if __name__ == "__main__":
    application = QtWidgets.QApplication([])

    window = Window()
    window.setWindowTitle("Window")
    window.setFixedSize(900, 900)
    window.show()

    sys.exit(application.exec_())

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.