1 from PyQt5.QtWidgets import * 2 from PyQt5 import QtCore,QtWidgets 3 from PyQt5.QtGui import * 4 5 import sys 6 class A(QWidget): 7 def __init__(self): 8 super(A,self).__init__() 9 self.setFixedSize(400,500) 10 self.layout=QGridLayout(self) 11 self.btn=QPushButton('添加') 12 self.layout.addWidget(self.btn) 13 self.setLayout(self.layout) 14 self.btn.clicked.connect(self.btn1) 15 def btn1(self): 16 label={} 17 ok,f=QFileDialog.getOpenFileNames(self,'打开','/','jpg(*.jpg)') 18 for i,j in enumerate(ok): 19 label[i]=QLabel(str(i)) 20 label[i].setFixedSize(500,500) 21 self.layout.addWidget(label[i]) 22 pix=QPixmap(j) 23 label[i].setPixmap(pix) 24 self.resize(pix.width(),pix.height()) 25 if __name__ == '__main__': 26 app = QApplication(sys.argv) 27 a = A() 28 a.show() 29 sys.exit(app.exec_())