显示多张图片

时间:2018-12-09 06:27:09

标签: python python-3.x pyqt pyqt4

我是PyQt4的新手,我试图在QScrollArea中显示图像,但我只设法显示一个图像,并且始终显示最后一个图像。如何在QScrollArea中显示多个图像?有更好的方法来显示多张图像吗?

这是我的代码:

for dog in dogs:  
    if dog.age == 15:
        #just print name of dog
        print(dog.name)
        #print the dog object
        print(dog)

1 个答案:

答案 0 :(得分:2)

QScrollArea您只能设置一个小部件,但是如果要显示多个小部件,则必须使用该单个小部件的布局来放置它。另一方面,在代码中,您使用的是单个QLabel,在循环中,您仅更改图像,因此您只能看到最后一张图像。

考虑到上述情况,解决方案如下:

import os
from PyQt4 import QtCore, QtGui

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        url = # ...
        highlight_dir = url + '\\highlighted'

        self.scrollArea = QtGui.QScrollArea(widgetResizable=True)
        self.setCentralWidget(self.scrollArea)
        content_widget = QtGui.QWidget()
        self.scrollArea.setWidget(content_widget)
        lay = QtGui.QVBoxLayout(content_widget)

        for file in os.listdir(highlight_dir):
            pixmap = QtGui.QPixmap(os.path.join(highlight_dir, file))
            if not pixmap.isNull():
                label = QtGui.QLabel(pixmap=pixmap)
                lay.addWidget(label)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

另一方面,如果文件夹中有很多图像,则该小部件将延迟显示,并且可能使用户不满意,一种可能的选择是使用带有QTimer的迭代来一点一点地加载图像。 / p>

import os
from PyQt4 import QtCore, QtGui

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        url = # ...
        highlight_dir = url + '\\highlighted'

        self.scrollArea = QtGui.QScrollArea(widgetResizable=True)
        self.setCentralWidget(self.scrollArea)
        content_widget = QtGui.QWidget()
        self.scrollArea.setWidget(content_widget)
        self._lay = QtGui.QVBoxLayout(content_widget)

        self.files_it = iter([os.path.join(highlight_dir, file) for file in os.listdir(highlight_dir)])

        self._timer = QtCore.QTimer(self, interval=1)
        self._timer.timeout.connect(self.on_timeout)
        self._timer.start()

    def on_timeout(self):
        try:
            file = next(self.files_it)
            pixmap = QtGui.QPixmap(file)
            self.add_pixmap(pixmap)
        except StopIteration:
            self._timer.stop()

    def add_pixmap(self, pixmap):
        if not pixmap.isNull():
            label = QtGui.QLabel(pixmap=pixmap)
            self._lay.addWidget(label)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

PyQt5:

import os
from PyQt5 import QtCore, QtGui, QtWidgets

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        url = # ...
        highlight_dir = url + '\\highlighted'

        self.scrollArea = QtWidgets.QScrollArea(widgetResizable=True)
        self.setCentralWidget(self.scrollArea)
        content_widget = QtWidgets.QWidget()
        self.scrollArea.setWidget(content_widget)
        self._lay = QtWidgets.QVBoxLayout(content_widget)

        self.files_it = iter([os.path.join(highlight_dir, file) for file in os.listdir(highlight_dir)])

        self._timer = QtCore.QTimer(self, interval=1)
        self._timer.timeout.connect(self.on_timeout)
        self._timer.start()

    def on_timeout(self):
        try:
            file = next(self.files_it)
            pixmap = QtGui.QPixmap(file)
            self.add_pixmap(pixmap)
        except StopIteration:
            self._timer.stop()

    def add_pixmap(self, pixmap):
        if not pixmap.isNull():
            label = QtWidgets.QLabel(pixmap=pixmap)
            self._lay.addWidget(label)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())