PyQt5每天必学之像素图控件QPixmap

yipeiwu_com7年前Python基础

QPixmap 像素图控件是用来处理图像的控件之一。它用于将优化后的图像显示在屏幕上。在我们的代码示例中,我们将使用QPixmap 控件在程序窗口上显示图像。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
PyQt5 教程

在这个例子中,我们显示窗口上的图像。

作者:我的世界你曾经来过
博客:http://blog.csdn.net/weiaitaowang
最后编辑:2016年8月4日
"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QLabel
from PyQt5.QtGui import QPixmap

class Example(QWidget):

  def __init__(self):
    super().__init__()

    self.initUI()

  def initUI(self):

    hbox = QHBoxLayout(self)
    pixmap = QPixmap('F:\Python\PyQt5\Widgets\images\liutao.png')

    lb1 = QLabel(self)
    lb1.setPixmap(pixmap)

    hbox.addWidget(lb1)
    self.setLayout(hbox)

    self.move(300, 300)
    self.setWindowTitle('像素图控件')    
    self.show()

  def showDate(self, date):

    self.lb1.setText(date.toString())

if __name__ == '__main__':

  app = QApplication(sys.argv)
  ex = Example()
  sys.exit(app.exec_())

在我们的例子中,我们将图像显示在该程序的窗口上。

pixmap = QPixmap('F:\Python\PyQt5\Widgets\images\liutao.png')

我们创建的QPixmap 对象需要一个文件作为参数。

lb1 = QLabel(self)
lb1.setPixmap(pixmap)

我们把QPixmap 对象映射到的QLabel 控件。

程序执行后

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Django ManyToManyField 跨越中间表查询的方法

1、在 django 表中用到了 manytomany 生成了中间表 pyclub_article_column from django.db import models # Cr...

python 处理string到hex脚本的方法

python 处理string到hex脚本的方法

实现目标:把文件1中数据如:B4A6C0ED69 处理后放入文件2:0XB4, 0XA6, 0XC0, 0XED, 0X69 V1.0代码如下(后续继续优化): #!/usr/bin...

Python3.5以上版本lxml导入etree报错的解决方案

Python3.5以上版本lxml导入etree报错的解决方案

在python中安装了lxml-4.2.1,在使用时发现导入etree时IDE中报错Unresolved reference 其实发现,不影响使用,可以正常运行,对于我这种要刨根问底的...

利用python修改json文件的value方法

做工程时遇到需要监听json文件,根据json文件中的key-value值作出相应处理的情形。为此写了修改json文件的python脚本供工程后续调用。 代码如下: # coding...

Django视图扩展类知识点详解

扩展类必须配合GenericAPIView使用扩展类内部的方法,在调用序列化器时,都是使用get_serializer 需要自定义get、post等请求方法,内部实现调用扩展类对应方法即...