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

yipeiwu_com6年前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 plotly画柱状图代码实例

python plotly画柱状图代码实例

这篇文章主要介绍了python plotly画柱状图代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码 import p...

在pytorch中查看可训练参数的例子

pytorch中我们有时候可能需要设定某些变量是参与训练的,这时候就需要查看哪些是可训练参数,以确定这些设置是成功的。 pytorch中model.parameters()函数定义如下:...

python提示No module named images的解决方法

本文讲述了python提示No module named images的解决方法,非常实用!分享给大家供大家参考。具体方法如下: 出现提示:ImportError: No module...

创建Shapefile文件并写入数据的例子

创建Shapefile文件并写入数据的例子

基本思路 使用GDAL创建Shapefile数据的基本步骤如下: 使用osgeo.ogr.Driver的CreateDataSource()方法创建osgeo.ogr.DataSourc...

Python安装Numpy和matplotlib的方法(推荐)

Python安装Numpy和matplotlib的方法(推荐) 注意: 下载的库名中cp27代表python2.7,其它同理。 在shell中输入import pip; print(p...