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

yipeiwu_com7年前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数据结构之栈、队列及二叉树定义与用法浅析

本文实例讲述了Python数据结构之栈、队列及二叉树定义与用法。分享给大家供大家参考,具体如下: 目前只实现了三种,栈、队列和二叉树,哪天得空继续补吧~ 1. 栈 #栈 class...

Python API 自动化实战详解(纯代码)

Python API 自动化实战详解(纯代码)

主要讲如何在公司利用Python 搞API自动化。 1.分层设计思路 dataPool :数据池层,里面有我们需要的各种数据,包括一些公共数据等 config :基础配置 tools :...

解决django-xadmin列表页filter关联对象搜索问题

环境:xadmin-for-python3 python3.5.2 django1.9.12 问题描述:Product ProductSku两个实体,ProductSku FK外键关联P...

CentOS安装pillow报错的解决方法

安装pillow出现以下问题: ValueError: jpeg is required unless explicitly disabled using --disable-jpe...

python 发送json数据操作实例分析

python 发送json数据操作实例分析

本文实例讲述了python 发送json数据操作。分享给大家供大家参考,具体如下: # !/usr/bin/env python # -*- coding: utf-8 -*-...