下面直接上代碼留存,方便以後查閱複用。
# -*- coding: utf-8 -*- #作者:LeniyTsan#時間:2014-07-17 import wxfrom 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調用。
程式啟動並執行結果如下: