【问题标题】:Show multiple images显示多张图片
【发布时间】:2018-12-09 06:27:09
【问题描述】:

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

这是我的代码:

scrollArea = QtGui.QScrollArea(self)
scrollArea.setWidgetResizable(False)
scrollArea.setGeometry(210, 150, 800, 450)
highlightLbl = QtGui.QLabel(self)
highlight_dir = url + '\\highlighted'
scrollArea = QtGui.QScrollArea(self)
scrollArea.setWidgetResizable(False)
scrollArea.setGeometry(210, 150, 800, 450)
for file in os.listdir(highlight_dir):
    highlighted_img = QtGui.QPixmap(os.path.join(highlight_dir, file))
    highlightLbl.setPixmap(highlighted_img)
    scrollArea.setWidget(highlightLbl) 

【问题讨论】:

    标签: python python-3.x pyqt pyqt4 qscrollarea


    【解决方案1】:

    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 的迭代来一点一点地加载图像。

    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_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 2016-10-28
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多