pyqt5 从本地选择图片 并显示在label上的实例

yipeiwu_com4年前Python基础

1.主要用到 QFileDialog 方法打开本地文件

2.界面

打开前:

打开后:

3. 代码

import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class picture(QWidget):
  def __init__(self):
    super(picture, self).__init__()

    self.resize(600, 400)
    self.setWindowTitle("label显示图片")

    self.label = QLabel(self)
    self.label.setText("  显示图片")
    self.label.setFixedSize(300, 200)
    self.label.move(160, 160)

    self.label.setStyleSheet("QLabel{background:white;}"
                 "QLabel{color:rgb(300,300,300,120);font-size:10px;font-weight:bold;font-family:宋体;}"
                 )

    btn = QPushButton(self)
    btn.setText("打开图片")
    btn.move(10, 30)
    btn.clicked.connect(self.openimage)
  def openimage(self):
    imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png;;All Files(*)")
    jpg = QtGui.QPixmap(imgName).scaled(self.label.width(), self.label.height())
    self.label.setPixmap(jpg)


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

以上这篇pyqt5 从本地选择图片 并显示在label上的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 装饰器功能以及函数参数使用介绍

python 装饰器功能以及函数参数使用介绍

简单的说:装饰器主要作用就是对函数进行一些修饰,它的出现是在引入类方法和静态方法的时候为了定义静态方法出现的。例如为了把foo()函数声明成一个静态函数 复制代码 代码如下: class...

python opencv之SURF算法示例

python opencv之SURF算法示例

本文介绍了python opencv之SURF算法示例,分享给大家,具体如下: 目标: SURF算法基础 opencv总SURF算法的使用 原理: 上节课使用了SIFT算法,...

Python3中正则模块re.compile、re.match及re.search函数用法详解

本文实例讲述了Python3中正则模块re.compile、re.match及re.search函数用法。分享给大家供大家参考,具体如下: re模块 re.compile、re.matc...

解决已经安装requests,却依然提示No module named requests问题

Python版本3.5.1, pip install requests 之后依然提示 Python ImportError: No module named 'requests' 经过文...

python selenium UI自动化解决验证码的4种方法

本文介绍了python selenium UI自动化解决验证码的4种方法,分享给大家,具体如下: 测试环境 windows7+ firefox50+ geckodrive...