Search code examples
wxpython

Panel repaints background image (using EVT_ERASE_BACKGROUND)


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.hideallpanels()
        self.startpanel.Show()
        #...  
        wx.StaticText(self.startpanel,-1,'Testtext',pos=(120,220))
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        #self.sizer.Add(self.panelnewgame, 1, wx.EXPAND)
        #self.sizer.Add(self.panelloadgame, 1, wx.EXPAND)
        self.SetSizer(self.sizer)    
        #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.SetSizer(hSizer)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

    def hideallpanels(self):
        self.startpanel.Hide()
        #...

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

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

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.


Solution

  • 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.