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

相关文章

下载糗事百科的内容_python版

复制代码 代码如下:#coding:utf-8 import urllib.request import xml.dom.minidom import sqlite3 import th...

python实现根据图标提取分类应用程序实例

本文实例讲述了python实现根据图标提取分类应用程序,分享给大家供大家参考。 具体方法如下: #!/usr/bin/python # -*- coding: utf-8 -*-...

Python ljust rjust center输出

看下面的例子就会明白了: 复制代码 代码如下:print '|','*'.ljust(10),'|' print '|','*'.ljust(10,'-'),'|' print '|',...

Python 判断是否为质数或素数的实例

一个大于1的自然数,除了1和它本身外,不能被其他自然数(质数)整除(2, 3, 5, 7等),换句话说就是该数除了1和它本身以外不再有其他的因数。 首先我们来第一个传统的判断思路:...

python Tcp协议发送和接收信息的例子

需要建立2个文件,一个作为客户端,一个作为服务端 文件一 作为客户端client,文件二作为服务端server 文件一 # client 客户端 # TCP必须建立连接 import...