Sen
Sen

Reputation: 35

Creating a button with a Bitmap image wxPython

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

Upvotes: 1

Views: 3799

Answers (2)

Rolf of Saxony
Rolf of Saxony

Reputation: 22433

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)

Upvotes: 0

Sen
Sen

Reputation: 35

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.

Upvotes: 1

Related Questions