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中的类与类型示例详解

1.经典类与新式类 在了解Python的类与类型前,需要对Python的经典类(classic classes)与新式类(new-style classes)有个简单的概念。 在Pyth...

wxpython中自定义事件的实现与使用方法分析

本文实例讲述了wxpython中自定义事件的实现与使用方法。分享给大家供大家参考,具体如下: 创建自定义事件的步骤: ① 定义事件类,该事件类必须继承自wx.PyCommandEvent...

利用Python将每日一句定时推送至微信的实现方法

利用Python将每日一句定时推送至微信的实现方法

前言 前几天在网上看到一篇文章《教你用微信每天给女票说晚安》,感觉很神奇的样子,随后研究了一下,构思的确是巧妙。好,那就开始动工吧!服务器有了,Python环境有了,IDE打开了...然...

如何使用python操作vmware

import pysphere from pysphere import VIServer host_ip = "200.200.173.45" username = "admi...

python简单实现基于SSL的IRC bot实例

本文实例讲述了python简单实现基于SSL的 IRC bot。分享给大家供大家参考。具体如下: #!/usr/bin/python # -*- coding: utf8 -*- i...