将图片文件嵌入到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产生随机的二维数组实例详解

最近找遍了python的各个函数发现无法直接生成随机的二维数组,其中包括random()相关的各种方法,都没有得到想要的结果。最后在一篇博客中受到启发,通过列表解析的方法得到随机的二维数...

pandas分别写入excel的不同sheet方法

pandas可以非常方便的写数据到excel,那么如何写多个dataframe到不同的sheet呢? 使用pandas.ExcelWriter import pandas as pd...

python pands实现execl转csv 并修改csv指定列的方法

如下所示: # -*- coding: utf-8 -*- import os import pandas as pd import numpy as np #from os im...

在Python中使用SQLite的简单教程

SQLite是一种嵌入式数据库,它的数据库就是一个文件。由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成。...

python Matplotlib画图之调整字体大小的示例

python Matplotlib画图之调整字体大小的示例

一张字体调整好的示例图: 字体大小就是 fontsize 参数 import matplotlib.pyplot as plt # 代码中的“...”代表省略的其他参数 ax =...