Search code examples
pythonwxpythonwxwidgets

Hide the "Failed to load" message when loading an invalid image, wxpython


bmp = wx.Image("C:\User\Desktop\cool.bmp", wx.BITMAP_TYPE_ANY).ConvertToBitmap()

If i run this, it will automatically show an error message saying that it failed to load the image. How can I stop my program from doing this?


Solution

  • I can't even get my wxPython code to run if I pass it an an invalid image. This is probably related to the fact that wxPython is a light wrapper around a C++ library though. See http://wiki.wxpython.org/C%2B%2B%20%26%20Python%20Sandwich for an interesting explanation.

    The best way around issues like this is to actually use Python's os module, like this:

    if os.path.exists(path):
       # then create the widget
    

    I do this sort of thing for config files and other things. If the file doesn't exist, I either create it myself or don't create the widget or I show a message so I know to fix it.