显示多张图片
Show multiple images
我是 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)
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_())
我是 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)
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_())