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

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

相关文章

Python3中urlencode和urldecode的用法详解

在Python3中,将中文进行urlencode编码使用函数 urllib.parse.quote(string, safe='/', encoding=None, errors=N...

Python 字符串中的字符倒转

方法一,使用[::-1]: s = 'python' print s[::-1] 方法二,使用reverse()方法: l = list(s) l.reverse() print ''....

python flask安装和命令详解

Flask Web开发实战学习笔记 Flask简介 Flask是使用Python编写的Web微框架。Web框架可以让我们不用关 心底层的请求响应处理,更方便高效地编写Web程序。因为Fl...

Windows下的Jupyter Notebook 安装与自定义启动(图文详解)

Windows下的Jupyter Notebook 安装与自定义启动(图文详解)

【听图阁-专注于Python设计】小编注:如果不是特殊需要建议安装 Anaconda3 即可,自带Jupyter Notebook 。 手动安装之前建议查看这篇文章:/post/1351...

给Python中的MySQLdb模块添加超时功能的教程

使用Python操作MySQL数据库的时候常使用MySQLdb这个模块。 今天在开发的过程发现MySQLdb.connect有些参数没法设置。通过这个页面我们可以看到在connect的时...