将图片文件嵌入到wxpython代码中的实现方法

yipeiwu_com6年前Python基础

下面直接上代码留存,方便以后查阅复用。

# -*- coding: utf-8 -*- 
#作者:LeniyTsan
#时间:2014-07-17
 
import wx
from wx.lib.embeddedimage import PyEmbeddedImage
 
class MyFrame1 ( wx.Frame ):
  def __init__( self, parent ):
    wx.Frame.__init__ ( self, parent )
    self.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_3DLIGHT ) )
    bSizer1 = wx.BoxSizer( wx.VERTICAL )
    file = open('author.png', 'rb')
    b64 = file.read().encode('base64')
    file.close()
    bitmap = PyEmbeddedImage(b64).GetBitmap()
    self.m_bitmap1 = wx.StaticBitmap( self, wx.ID_ANY, bitmap )
    bSizer1.Add( self.m_bitmap1, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
    self.SetSizer( bSizer1 )
    self.Layout()
    bSizer1.Fit( self )
    self.Centre( wx.BOTH )
app = wx.App()
gui = MyFrame1(None)
gui.Show()
app.MainLoop() 

重点部分是bitmap = PyEmbeddedImage(b64).GetBitmap()代码,其中b64是前面生成的图片的base64字符串,bitmap就是我们的图片对象,可以让wx.StaticBitmap调用。

程序运行的结果如下:

相关文章

python定位xpath 节点位置的方法

chrome 右键有copy xpath地址 但是有些时候获取的可能不对 可以自己用代码验证一下 如果还是不行 可以考虑从源码当中取出来 趁热打铁,使用前一篇文章中 XPath 节点来定...

python+selenium 定位到元素,无法点击的解决方法

报错 selenium.common.exceptions.WebDriverException: Message: Element is not clickable at poin...

python集合是否可变总结

集合是一个无序的可变的序列。集合中的元素必须是可hash的,即不可变的数据类型。 空集合 a=set() 注意a={}创建的是一个空字典。 set —— 可变集合。集合中的元素可以动态...

TensorFlow实现简单的CNN的方法

TensorFlow实现简单的CNN的方法

这里,我们将采用Tensor Flow内建函数实现简单的CNN,并用MNIST数据集进行测试 第1步:加载相应的库并创建计算图会话 import numpy as np import...

Python中的字符串替换操作示例

字符串的替换(interpolation), 可以使用string.Template, 也可以使用标准字符串的拼接. string.Template标示替换的字符, 使用"$"符号, 或...