如何加载与PySide的图像的接触表



下面的代码加载一张图片。我希望加载一个未知数量的图像驻留在一个文件夹中,我希望它们以一种联系表的方式显示。

我如何修改下面的代码,使它将采取的图像列表,并显示他们并排?

import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
    def initUI(self):      
        hbox = QtGui.QHBoxLayout(self)
        pixmap = QtGui.QPixmap("myImage.jpg")
        lbl = QtGui.QLabel(self)
        lbl.setPixmap(pixmap)
        hbox.addWidget(lbl)
        self.setLayout(hbox)
        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Image viewer')
        self.show()        
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

这可能是你想要的整体的东西,包括滚动条。

import os
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
    def initUI(self):
        self.img_fold = r"C:Usersabhishek.gargDesktopNew folder"
        self.widget_layout = QtGui.QVBoxLayout(self)
        self.scrollarea = QtGui.QScrollArea()
        self.scrollarea.setWidgetResizable(True)
        self.widget_layout.addWidget(self.scrollarea)
        self.widget = QtGui.QWidget()
        self.layout = QtGui.QVBoxLayout(self.widget)
        self.scrollarea.setWidget(self.widget)
        self.layout.setAlignment(QtCore.Qt.AlignHCenter)
        for img in os.listdir(self.img_fold):
            img_path = os.path.join(self.img_fold, img)
            pixmap = QtGui.QPixmap(img_path)
            lbl = QtGui.QLabel(self)
            lbl.setPixmap(pixmap)
            self.layout.addWidget(lbl)

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Image viewer')
        self.show()
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

try this:

import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
    def initUI(self):
        hbox = QtGui.QHBoxLayout(self)

        img_fold = "C:/my_contacts"
        for img in os.listdir(img_fold):
            img_path = os.path.join(img_fold, img)
            pixmap = QtGui.QPixmap(img_path)
            lbl = QtGui.QLabel(self)
            lbl.setPixmap(pixmap)
            hbox.addWidget(lbl)
        self.setLayout(hbox)
        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Image viewer')
        self.show()
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

最新更新