views:

174

answers:

2

I have a module which starts a wxPython app, which loads a wx.Bitmap from file for use as a toolbar button. It looks like this: wx.Bitmap("images\\new.png", wx.BITMAP_TYPE_ANY). All works well when I run that module by itself, but when I try to import and run it from a different module which is in a different directory, wxPython raises an exception. (The exception is something internal regarding the toolbar, which I think just means it's not loading the bitmap right.)

What should I do?

+1  A: 

"images\new.png" is a relative path, so when bitmap gets loaded it will depened what is the cur dir so either you set cur dir

os.chdir("location to images folder")

or have a function which loads relative to your program e.g.

def getProgramFolder():
    moduleFile = __file__
    moduleDir = os.path.split(os.path.abspath(moduleFile))[0]
    programFolder = os.path.abspath(moduleDir)
    return programFolder

bmpFilePath = os.path.join(getProgramFolder(), "images\\new.png")
Anurag Uniyal
Is this standard practice when making an application with wxPython?
cool-RR
I will say this is standard practice in any application you want to use relative paths, somewhere you must tell what is this relative too
Anurag Uniyal
else you can embed image into app, but you loose flexibility during development,
Anurag Uniyal
A: 

The wxPython FAQ recommends using a tool called img2py.py to embed icon files into a Python module. This tool comes with the wxPython distribution.

Here is an example of embedding toolbar icons.

Ayman Hourieh