Loading a bitmap for use in wx

I'd like to create some custom bitmaps for use with my buttons. Can
someone point me to an example of how to do that? My preferred method
would be to load a .png file. Most of the examples have been those
strange giant strings, which I'd rather not use. Still, if that's the
only way, I'd like to know how to do it...
Thanks
Ivan

Hi,

···

On Thu, Feb 25, 2010 at 1:46 PM, sunspider <sunspider@gmail.com> wrote:

I'd like to create some custom bitmaps for use with my buttons. Can
someone point me to an example of how to do that? My preferred method
would be to load a .png file. Most of the examples have been those
strange giant strings, which I'd rather not use. Still, if that's the
only way, I'd like to know how to do it...

Search the interwebs: http://lmgtfy.com/?q=wxpython+bitmap

bmp = wx.Bitmap("/path/to/your/image.png", wx.BITMAP_TYPE_PNG)

Cody

The "giant strings" are simply PNG files embedded into Python code in a way that they can be loaded as PNGs again at runtime. The advantages of doing it that way is that you don't have to worry about finding where the files are at on disk if your app is ever run from a location different than where you run it from, you don't have to worry about distributing anything but Python modules, etc. If you ever want to create your own "giant strings" for your images then take a look at the img2py tool.

···

On 2/25/10 11:46 AM, sunspider wrote:

I'd like to create some custom bitmaps for use with my buttons. Can
someone point me to an example of how to do that? My preferred method
would be to load a .png file. Most of the examples have been those
strange giant strings, which I'd rather not use. Still, if that's the
only way, I'd like to know how to do it...

--
Robin Dunn
Software Craftsman

Hi, as a newbie I had the same question.
Here is how I do it (you can load most type of images of, of any
size).
Be sure also to check out the icon from wx.artprovider: it contains
some nice ones.
I am not sure how to post an attachment here, so you might have to do
some indentation corrections. BTW loading a icon from python source is
trivial to.

import wx
ID_S1 = wx.NewId()
ID_S2 = wx.NewId()
ID_S3 = wx.NewId()
ID_testFrame = wx.NewId()
#change this to the right path on your pc
Const_Drive = "D:\\"
Const_BaseDir = Const_Drive + "___python.excel\\"
Const_IconDir = Const_BaseDir + "icons\\"

class startCtrl(wx.Panel):
    #this class is only for constructing a toolbar
    def __init__(self, parent, frame, id=wx.ID_ANY,
pos=wx.DefaultPosition,
                 size=wx.DefaultSize, mgr=None):
        wx.Panel.__init__(self, parent, id, pos, size)
        self.bar= wx.ToolBar(self, -1, wx.DefaultPosition,
wx.DefaultSize,
                             wx.TB_NODIVIDER | wx.TB_HORZ_TEXT)
        self.bar.SetToolBitmapSize(wx.Size(48,48))
        caption, symbol = "dummy",
wx.ArtProvider_GetBitmap(wx.ART_ERROR)
        self.bar.AddLabelTool(ID_S1,caption, symbol)
        symbol = getIconFromFile("A_THUNDER.ico", (32,32))
        self.bar.AddLabelTool(ID_S2," from ico file ",symbol)
        symbol = getIconFromFile("A_REPEAT.png", (24,24))
        self.bar.AddLabelTool(ID_S3," From PNG ", symbol)

        self.bar.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL,
False, 'Arial'))
        self.bar.Realize()

        sizer = wx.GridSizer(vgap=0, hgap=0)
        sizer.Add(wx.Panel(self, -1))
        sizer.Add(self.bar)
        self.SetSizer(sizer)
        self.Fit()

class testFrame(wx.Frame):
    #handler class all toolbar events are ment to be used here
    def __init__(self, parent, id=-1, title=""):
        wx.Frame.__init__(self, parent, id)
        self.Maximize()
        self.ctrl = startCtrl(self, self)
        self.Bind(wx.EVT_MENU_RANGE, self.OnButton,
id=ID_S1,id2=ID_S3)
    def OnButton(self,event):
        xid = event.GetId()
        pass
    def OnExit(self, event):
        self.Close()
def getIconFromFile(iconFile, size=(48,48)):
    selectFile =Const_IconDir+iconFile
    try:
        wat2 = wx.Image(selectFile, wx.BITMAP_TYPE_ANY)
        wat2.Rescale(size[0], size[1])
        return wat2.ConvertToBitmap()
    except:
        assert True, "error loading"+ selectFile

···

#----------------------------------------------------------------------------
class testApp(wx.App):
    def __init__(self, name):
        self.name = name
        wx.App.__init__(self, redirect=False)
    def OnInit(self):
        frame = testFrame(None, ID_testFrame)
        frame.Maximize()
        frame.Show()
        return True

if __name__ == '__main__':
    app = testApp('test')
    app.MainLoop()

Thanks for the help everyone. I got it.
Sorry about being ignorant about the encoded PNGs. That's a cool
feature and everything, it just doesn't suit my purposes.

···

On Feb 25, 1:11 pm, samuel en <samuel110...@gmail.com> wrote:

Hi, as a newbie I had the same question.
Here is how I do it (you can load most type of images of, of any
size).
Be sure also to check out the icon from wx.artprovider: it contains
some nice ones.
I am not sure how to post an attachment here, so you might have to do
some indentation corrections. BTW loading a icon from python source is
trivial to.

import wx
ID_S1 = wx.NewId()
ID_S2 = wx.NewId()
ID_S3 = wx.NewId()
ID_testFrame = wx.NewId()
#change this to the right path on your pc
Const_Drive = "D:\\"
Const_BaseDir = Const_Drive + "___python.excel\\"
Const_IconDir = Const_BaseDir + "icons\\"

class startCtrl(wx.Panel):
#this class is only for constructing a toolbar
def __init__(self, parent, frame, id=wx.ID_ANY,
pos=wx.DefaultPosition,
size=wx.DefaultSize, mgr=None):
wx.Panel.__init__(self, parent, id, pos, size)
self.bar= wx.ToolBar(self, -1, wx.DefaultPosition,
wx.DefaultSize,
wx.TB_NODIVIDER | wx.TB_HORZ_TEXT)
self.bar.SetToolBitmapSize(wx.Size(48,48))
caption, symbol = "dummy",
wx.ArtProvider_GetBitmap(wx.ART_ERROR)
self.bar.AddLabelTool(ID_S1,caption, symbol)
symbol = getIconFromFile("A_THUNDER.ico", (32,32))
self.bar.AddLabelTool(ID_S2," from ico file ",symbol)
symbol = getIconFromFile("A_REPEAT.png", (24,24))
self.bar.AddLabelTool(ID_S3," From PNG ", symbol)

    self\.bar\.SetFont\(wx\.Font\(12, wx\.SWISS, wx\.NORMAL, wx\.NORMAL,

False, 'Arial'))
self.bar.Realize()

    sizer = wx\.GridSizer\(vgap=0, hgap=0\)
    sizer\.Add\(wx\.Panel\(self, \-1\)\)
    sizer\.Add\(self\.bar\)
    self\.SetSizer\(sizer\)
    self\.Fit\(\)

class testFrame(wx.Frame):
#handler class all toolbar events are ment to be used here
def __init__(self, parent, id=-1, title=""):
wx.Frame.__init__(self, parent, id)
self.Maximize()
self.ctrl = startCtrl(self, self)
self.Bind(wx.EVT_MENU_RANGE, self.OnButton,
id=ID_S1,id2=ID_S3)
def OnButton(self,event):
xid = event.GetId()
pass
def OnExit(self, event):
self.Close()
def getIconFromFile(iconFile, size=(48,48)):
selectFile =Const_IconDir+iconFile
try:
wat2 = wx.Image(selectFile, wx.BITMAP_TYPE_ANY)
wat2.Rescale(size[0], size[1])
return wat2.ConvertToBitmap()
except:
assert True, "error loading"+ selectFile

#----------------------------------------------------------------------------
class testApp(wx.App):
def __init__(self, name):
self.name = name
wx.App.__init__(self, redirect=False)
def OnInit(self):
frame = testFrame(None, ID_testFrame)
frame.Maximize()
frame.Show()
return True

if __name__ == '__main__':
app = testApp('test')
app.MainLoop()