【问题标题】:How to load a contact sheet of images with PySide?如何使用 PySide 加载图像的联系表?
【发布时间】:2013-06-03 15:40:09
【问题描述】:

下面的代码加载一张图片。我希望加载位于文件夹中的未知数量的图像,并希望它们以联系表​​的方式显示。

如何修改下面的代码,以便获取图像列表并并排显示它们?

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

【问题讨论】:

    标签: python widget pyside qpixmap


    【解决方案1】:

    这可能是您想要的整体内容,包括滚动条..

    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:\Users\abhishek.garg\Desktop\New 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()
    

    【讨论】:

    • 哦,这太完美了——我需要滚动条 :)
    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 我还建议您使用网格布局,以防您想要制作相册之类的东西,而且您必须使您的框可滚动,否则您将无法使用它它从屏幕上消失了..有意义吗??
    • 有关如何使用包含数千张图像的文件夹以及您可能希望立即显示窗口然后让图像填充窗口的任何指针,因为您可以滚动浏览它?
    • 我建议您使用分页样式而不是一次加载所有图像,因为那样会非常沉重,请使用堆叠小部件并在单击新页面时添加新页面。 .
    猜你喜欢
    • 1970-01-01
    • 2015-11-26
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多