pyqt5读取本地多张图片并显示在label

pyqt5读取本地多张图片并显示在label
看了很多博客以及文档,关于pyqt5的资料大多写的模棱两可,废话不多说,show you my code!

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ui_openimage.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1144, 750)
        self.label_1 = QtWidgets.QLabel(Form)
        self.label_1.setGeometry(QtCore.QRect(130, 130, 351, 251))
        self.label_1.setObjectName("label_1")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(680, 140, 351, 251))
        self.label_2.setObjectName("label_2")
        self.btn_image = QtWidgets.QPushButton(Form)
        self.btn_image.setGeometry(QtCore.QRect(170, 560, 93, 28))
        self.btn_image.setObjectName("btn_image")

        self.retranslateUi(Form)
        self.btn_image.clicked.connect(self.slot_open_image)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label_1.setText(_translate("Form", "TextLabel"))
        self.label_2.setText(_translate("Form", "TextLabel"))
        self.btn_image.setText(_translate("Form", "选择图片"))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/3/31 11:13
# @Author  : EricWei
# @Site    : 
# @File    : 11.py
# @Software: PyCharm


from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtGui import QPixmap
from ui_openimage import Ui_Form
import sys
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplication
import os,sys


class window(QtWidgets.QMainWindow,Ui_Form):
    def __init__(self):
        super(window, self).__init__()
        self.cwd = os.getcwd()
        self.setupUi(self)
        self.labels = [self.label_1, self.label_2]
    def slot_open_image(self):
        files, filetype = QFileDialog.getOpenFileNames(self, '打开多个图片', self.cwd, "*.jpg, *.png, *.JPG, *.JPEG, All Files(*)")
        for i in range(len(files)):
            jpg = QtGui.QPixmap(files[i]).scaled(self.labels[i].width(), self.labels[i].height())
            self.labels[i].setPixmap(jpg)

if __name__ == "__main__":
  app = QtWidgets.QApplication(sys.argv)
  my = window()
  my.show()
  sys.exit(app.exec_())


pyqt5读取本地多张图片并显示在label_第1张图片

你可能感兴趣的:(python,pyqt5,python,pyqt)