Introduction

This recipe provides a small app that demonstrates some of the basics of using a wx.StaticBitmap, particularly how to change the bitmap displayed.

What Objects are Involved

Process Overview

The recipe puts a wx.StaticBitmap on a wx.Frame and provides a wx.Button that changes the image.

Special Concerns

Code Sample

import wx, os

class TestFrame(wx.Frame):
    def __init__(self, *args, **kwargs):

        wx.Frame.__init__(self, *args, **kwargs)
        # there needs to be an "Images" directory with one or more jpegs in it in the
        # current working directory for this to work
        self.jpgs = GetJpgList("./Images") # get all the jpegs in the Images directory
        self.CurrentJpg = 0

        self.MaxSize = 200

        b = wx.Button(self, label= "Display next")
        self.Bind(wx.EVT_BUTTON, self.DisplayNext)
        # starting with an EmptyBitmap, the real one will get put there
        # by the call to .DisplayNext()
        self.Image = wx.StaticBitmap(self, bitmap=wx.EmptyBitmap(self.MaxSize, self.MaxSize))
        self.DisplayNext()

        # Using a Sizer to handle the layout: I never like to use absolute postioning
        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(b, 0, wx.CENTER|wx.ALL, 10)

        # adding stretchable space before and after centers the image.
        box.Add((1,1),1)
        box.Add(self.Image, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALL|wx.ADJUST_MINSIZE, 10)
        box.Add((1,1),1)

        self.SetSizerAndFit(box)

        wx.EVT_CLOSE(self, self.OnCloseWindow)

    def DisplayNext(self, event=None):
        # load the image
        Img = wx.Image(self.jpgs[self.CurrentJpg], wx.BITMAP_TYPE_JPEG)

        # scale the image, preserving the aspect ratio
        W = Img.GetWidth()
        H = Img.GetHeight()
        if W > H:
            NewW = self.MaxSize
            NewH = self.MaxSize * H / W
        else:
            NewH = self.MaxSize
            NewW = self.MaxSize * W / H
        Img = Img.Scale(NewW,NewH)

        # convert it to a wx.Bitmap, and put it on the wx.StaticBitmap
        self.Image.SetBitmap(wx.BitmapFromImage(Img))

        # You can fit the frame to the image, if you want.
        self.Fit()

        self.CurrentJpg += 1
        if self.CurrentJpg > len(self.jpgs) -1:
            self.CurrentJpg = 0

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


def GetJpgList(dir):
    jpgs = [f for f in os.listdir(dir) if f[-4:] == ".jpg"]
    print "JPGS are:", jpgs

    return [os.path.join(dir, f) for f in jpgs]

class App(wx.App):
    def OnInit(self):
        frame = TestFrame(None, title="wxBitmap Test")
        frame.Show(True)
        return True

if __name__ == "__main__":
    app = App(0)
    app.MainLoop()

Comments

Please test this out on other platforms and/versions. If it doesn't work find a fix and correct it here, or send me a note:

Chris.Barker@noaa.gov

- I have tried it on Windows XP Professional, and it works. - Freek Sanders - Also Win2K Pro. - Bill Bell

wxStaticBitmap (last edited 2008-03-11 10:50:30 by localhost)