python框架qt如何显示多张图片
QscrollArea QPixmap Qtimer
显示图片是我们经常使用到的功能,如何在pyqt中显示图片呢?我们可以使用label来承载图片。那么多张图片如何显示呢?答案是我们可以使用QScrollArea来承载多张图片。
import os
import sys
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'
highlight_dir = url
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__':
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())