Munchkin
Munchkin

Reputation: 4956

Loading a static Bitmap

how can I load a simple, static image (Bitmap) into my panel? I just want to define the position of the image, nothing else.

This runs without error:

bmp = wx.Image('pics/default.bmp', wx.BITMAP_TYPE_BMP).ConvertToBitmap()
wx.StaticBitmap(self.panel, -1, bmp, (500, 500))

But instead of my image (default.bmp) there is just a small, black square (but at least at the correct position)

Edit: This works:

    self.picture = wx.StaticBitmap(self.panel,size=(200,300),pos=(500,500))
    self.picture.SetBitmap(wx.Bitmap('pics/default.bmp'))

The problem was that I had to add the size of my StaticBitmap.

Upvotes: 2

Views: 6323

Answers (1)

acattle
acattle

Reputation: 3113

I'm not too familiar with inserting bitmaps into wxPython however, I just did a bit of reading on the API and I think I may have found your problem.

The API for wx.StaticBitmap.__init__() states "Normally you should only call this from a subclass' init as a plain old wx.Control is not very useful." On the same page you can also see a wx.StaticBitmap.Create() method which is described as "Do the 2nd phase and create the GUI control." You're only completing the first phase of a two phase process. Try calling the Create() method and see if it helps.

The question then becomes "why do we even need a 'phase 2' for creation?" I did a bit more digging and I found the following excerpt from the wx.Image API:

A wx.Image cannot be drawn directly to a wx.DC. Instead, a platform-specific wx.Bitmap object must be created from it using the wx.BitmapFromImage constructor.

This seems to imply that there is some platform-dependent part of image rendering in wxPython that either required, or logically warranted, that image creation be separated into two phases.

One final note is that, as you can see, the wx.Image API says to use a wx.BitmapFromImage() method. I read the documentation and it looks like it might do all the work for you.

Good luck!

Upvotes: 3

Related Questions