Take the 2-minute tour ×

I have a frame with a few different panels and every panel shall get the same background image. So I found this example: http://www.blog.pythonlibrary.org/2010/03/18/wxpython-putting-a-background-image-on-a-panel/ I copied the "def OnEraseBackground(self, evt)" from this example and Bind it to EVT_ERASE_BACKGROUND. Result: The panel repaints the background image with the colour grey. My background image has the same width, but it is higher than the panel. So here you can see that the panel repaints the image: http://s14.directupload.net/file/d/3030/ej22mwbx_jpg.htm (the green bottom part is part of my image; the grey rectangle is my panel). I also added these lines to my __init__:

import wx

class MyForm(wx.Frame):
    def __init__(self):    
        wx.Frame.__init__(self, None, wx.ID_ANY, "Click Kick",size=(1124,750),pos=((wx.DisplaySize()[0]-1024)/2,10))
        self.startpanel = wx.Panel(self,size=(1024,600))
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        #self.sizer.Add(self.panelnewgame, 1, wx.EXPAND)
        #self.sizer.Add(self.panelloadgame, 1, wx.EXPAND)
        #hSizer = wx.BoxSizer(wx.HORIZONTAL)
        #hSizer.Add((1,1), 1, wx.EXPAND)
        #hSizer.Add(self.sizer, 0, wx.TOP, 100)
        #hSizer.Add((1,1), 0, wx.ALL, 75)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

    def hideallpanels(self):

    def OnEraseBackground(self, evt):
        dc = evt.GetDC()
        if not dc:
            dc = wx.ClientDC(self)
            rect = self.GetUpdateRegion().GetBox()
        bmp = wx.Bitmap("background.bmp")
        dc.DrawBitmap(bmp, 0, 0)

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()

But with these lines I can't start my program: No error message, but it just don't start.

Edit: There is no need to resize the background image (or something else) when the window gets resized. the window is static and shouldn't get resized.

Edit2: I updated the code example above with all code that could be relevant.

Edit3: Sorry, but now it runs. You just need a background image called "background.bmp" which should be bigger than the wx.Panel (or you lower the size of the wx.Panel) -> Just to see, that the panel repaints the image.

share|improve this question
I think you'll need to provide a complete small, runnable example. Did you set the panel's background style to wx.BG_STYLE_CUSTOM? And did you bind the EVT_ERASE_BACKGROUND to the panel? –  Mike Driscoll Oct 1 '12 at 13:38
I updated my code example. –  Munchkin Oct 1 '12 at 13:59
That is far from runnable –  Mike Driscoll Oct 1 '12 at 14:09
Sorry, but now it runs. =) –  Munchkin Oct 1 '12 at 14:41

1 Answer 1

up vote 1 down vote accepted

The problem is a parenting issue. If you look at the rest of my tutorial you'll notice that I'm binding the EVT_ERASE_BACKGROUND to the panel, NOT the frame. Also, the OnEraseBackground handler is updating the panel, not the frame, so you'll need to change a couple of lines:

class MyForm(wx.Frame):
    def __init__(self):
        # change the last line in the init
        self.startpanel.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

    def OnEraseBackground(self, evt):
        dc = evt.GetDC()
        if not dc:
            dc = wx.ClientDC(self)
            # change this line too!!
            rect = self.startpanel.GetUpdateRegion().GetBox()

You'll probably want to use the frame's SetSizeHints method to set the maximum and minimum resize limits though. Otherwise when you resize the frame larger than the image, you'll see gray on the borders.

share|improve this answer
Thank you very much, now my image is shown on my "startpanel". But there are still 2 problems remaining: 1st) The image is only shown on my startpanel, but I have some more panels which should receive the same background image. Do I have to Bind the EVT_ERASE_BACKGROUND to each of these panels or is there another way to solve this? 2nd) As you see, there is a StaticText on the panel. And there is a grey rectangle around this StaticText... is it possible, to delete this grey rectangle/make it transparent? –  Munchkin Oct 2 '12 at 13:35
I think you have to apply the same technique to all panels as each is its own instance. You could create a base panel with the background image and then subclass that for all you panels though. As for the StaticText, I'm not sure, but I doubt it. If you want the text to not have a background, you'll probably need to draw it yourself. –  Mike Driscoll Oct 2 '12 at 13:45
OK, thank you! btw: I just figured out that I also have to change this line: self.startpanel.SetSizer(hSizer) –  Munchkin Oct 2 '12 at 14:12
No problem. Sorry I missed that bit. –  Mike Driscoll Oct 2 '12 at 14:25

Your Answer


