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

yipeiwu_com5年前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设计】。

相关文章

CentOS6.9 Python环境配置(python2.7、pip、virtualenv)

python2.7 yum install -y zlib zlib-devel openssl openssl-devel mysql-devel gcc gcc-c++ wget...

Python基于lxml模块解析html获取页面内所有叶子节点xpath路径功能示例

本文实例讲述了Python基于lxml模块解析html获取页面内所有叶子节点xpath路径功能。分享给大家供大家参考,具体如下: 因为需要使用叶子节点的路径来作为特征,但是原始的lxml...

Python操作MySQL数据库的方法

pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同。 下载安装 pip3 install pymysql 使用操作 1、执行SQL i...

python使用wmi模块获取windows下的系统信息 监控系统

Python用WMI模块获取Windows系统的硬件信息:硬盘分区、使用情况,内存大小,CPU型号,当前运行的进程,自启动程序及位置,系统的版本等信息。 本文实例讲述了python使用...

Python 性能优化技巧总结

1.使用测量工具,量化性能才能改进性能,常用的timeit和memory_profiler,此外还有profile、cProfile、hotshot等,memory_profiler用了...