Ram Rachum
Ram Rachum

Reputation: 88718

wxPython: Path problems when exporting a bitmap

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?

Upvotes: 2

Views: 1240

Answers (2)

Ayman Hourieh
Ayman Hourieh

Reputation: 137416

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.

Upvotes: 1

Anurag Uniyal
Anurag Uniyal

Reputation: 88855

"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")

Upvotes: 3

Related Questions