1

I am using wxPython on Python 2.7. I would like some help with creating a button with bitmap images.

I am using this video https://www.youtube.com/watch?v=Y7f0a7xbWHI, and I followed the codes and typed

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Frame aka window',size=(300,200))
        panel=wx.Panel(self)

        pic=wx.Image("back.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        self.button=wx.BitmapButton(panel, -1, pic, pos=(10,10))
        self.Bind(wx.EVT_BUTTON, self.doMe, self.button)
        self.button.SetDefault()

    def doMe(self, event):
        self.Destroy

to create a button with an image. I got an error stating Invalid Image. I saved the bitmap image in a folder that has .py file I am working with. I feel like I am saving the image in the wrong place? Thank you in advance.

The error I received

Traceback (most recent call last):
  File "C:\Python27\FrameWindow.py", line 81, in <module>
    frame=bucky(parent=None,id=-1)
  File "C:\Python27\FrameWindow.py", line 17, in __init__
     pic=wx.Image("back.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
  File "C:\Python27\lib\site-packages\wx\core.py", line 708, in _Image_ConvertToBitmap
    bmp = wx.Bitmap(self, depth)
wxAssertionError: C++ assertion "image.IsOk()" failed at ..\..\src  \msw\bitmap.cpp(922) in wxBitmap::CreateFromImage(): invalid image
2
  • It's very helpful to include the error you received so that way other people can Google the error message and find it. Commented Feb 7, 2018 at 21:34
  • Thanks for letting me know. I'm a noob here. I just added the error codes. Commented Feb 7, 2018 at 21:53

2 Answers 2

1

I got it to work by adding

"locale = wx.Locale(wx.LANGUAGE_ENGLISH)"

under

class MainFrame(wx.Frame):
    def __init__(self):

now I do not get the error message, and it runs as it should. The error code I received for this problem was:

Traceback (most recent call last):
   File "C:\Python27\panel test.py", line 21, in <module>
      frame = MainFrame()
   File "C:\Python27\panel test.py", line 11, in __init__
      pic = wx.Bitmap("back.bmp", wx.BITMAP_TYPE_ANY)
 wxAssertionError: C++ assertion "strcmp(setlocale(LC_ALL, NULL), "C") == 0" failed at ..\..\src\common\intl.cpp(1579) in wxLocale::GetInfo(): You probably called setlocale() directly instead of using wxLocale and now there is a mismatch between C/C++ and Windows locale.
 Things are going to break, please only change locale by creating wxLocale objects to avoid this!

The error given by my primary question was solved by the codes given by Rolf by Saxony.

Sign up to request clarification or add additional context in comments.

1 Comment

You can accept an answer by clicking the tick mark beside it. You can mark an answer as Useful or Not Useful by clicking the Up/Down arrows beside it, respectively. Or you can accept your own answer (but you don't get any points for that). It's your call.
0

Use wx.BITMAP_TYPE_ANY rather than wx.BITMAP_TYPE_BMP it takes the guessing out of whether it really is a BMP or not
or use wx.Bitmap() directly.

import wx

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,'Frame aka window',size=(300,200))
        panel=wx.Panel(self)
        #
        # Use wx.BITMAP_TYPE_ANY rather than wx.BITMAP_TYPE_BMP
        #
        #pic=wx.Image("Discord.bmp", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        #
        # or use wx.Bitmap()
        pic = wx.Bitmap("Discord.bmp", wx.BITMAP_TYPE_ANY)
        #
        self.button=wx.BitmapButton(panel, -1, pic, pos=(10,10))
        self.Bind(wx.EVT_BUTTON, self.doMe, self.button)
        self.Show()

    def doMe(self, event):
        self.Destroy()

if __name__ == "__main__":
      app = wx.App()
      frame = MainFrame()
      app.MainLoop()

enter image description here

In principle you should name your image/s with a full pathname. If you have many images, then have an image directory and join that directory name to your image names as you use them, which again gives you a full path (/home/images/back_button.bmp)

5 Comments

Thanks for your answer, I tried your method, and now it says "wxAssertionError: C++ assertion "strcmp(setlocale(LC_ALL, NULL), "C") == 0" failed at ..\..\src\common\intl.cpp(1579) in wxLocale::GetInfo(): You probably called setlocale() directly instead of using wxLocale and now there is a mismatch between C/C++ and Windows locale. Things are going to break, please only change locale by creating wxLocale objects to avoid this!" What do I do in this case?
No Idea. My simple code above runs happily on Linux Mint 18.3, with no reference to locale, as you can see from the code. Did the error occur running the code above,as is, or did you apply the changes to your code? If the latter try running just the code above as a standalone program (changing the name of the image, obviously)
Yes, I tried running your code as is with my image. Is there any specific place I need to have my images saved to use on wxPython??
Also, my code seems to be working when I don't run the module on IDLE, but left-click the .py file name in the folder. The error message shows when I "run module" on IDLE, but when I left-click the file name, it has my picture and everything seems to be working.
I know nothing of IDLE. I code from the command line. In principle you should name your image/s with a full pathname. If you have many images, then have an image directory and join that directory name to your image names as you use them, which again gives you a full path (/home/images/back_button.bmp)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.