1

I am having some confusing behavior in wxpython. I just loaded the newest version (3.0.0.0) into a RHEL 6.4 with Python 2.6.6.

Most things seem to work, but my previous code for running a gui with a background image fails. All the buttons work, etc, but the background is just the default gray one.

I tried tracking down the problem by running this minimal example from Mike Driscoll's python website (http://www.blog.pythonlibrary.org/2010/03/18/wxpython-putting-a-background-image-on-a-panel/), and had the same problem with no background showing up other than the default gray one (I previously used his example on another machine with an older version of wxpython [2.8.12.1] with no problems):

import wx

########################################################################
class MainPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.frame = parent

        sizer = wx.BoxSizer(wx.VERTICAL)
        hSizer = wx.BoxSizer(wx.HORIZONTAL)

        for num in range(4):
            label = "Button %s" % num 
            btn = wx.Button(self, label=label)
            sizer.Add(btn, 0, wx.ALL, 5)
        hSizer.Add((1,1), 1, wx.EXPAND)
        hSizer.Add(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 OnEraseBackground(self, evt):
        """
        Add a picture to the background
        """
        # yanked from ColourDB.py
        dc = evt.GetDC()

        if not dc:
            dc = wx.ClientDC(self)
            rect = self.GetUpdateRegion().GetBox()
            dc.SetClippingRect(rect)
        dc.Clear()
        bmp = wx.Bitmap("butterfly.jpg")
        dc.DrawBitmap(bmp, 0, 0)


########################################################################
class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, size=(600,450))
        panel = MainPanel(self)
        self.Center()

########################################################################
class Main(wx.App):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, redirect=False, filename=None):
        """Constructor"""
        wx.App.__init__(self, redirect, filename)
        dlg = MainFrame()
        dlg.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = Main()
    app.MainLoop()

Now, when I run this, I don't get any errors, but I determined that the function onEraseBackground never seems to run. I don't know what is going wrong here; did wxpython 3.0.0.0 stop allowing this kind of image background setting procedure?

1 Answer 1

6

Try commenting out the

self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)

line. Some other people have experienced the same issue and this line was the culprit - it prevents the EVT_ERASE_BACKGROUND event being triggered.

You can also try replacing this line with

self.SetBackgroundStyle(wx.BG_STYLE_ERASE)

to ensure the erase events are triggered.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep. IIRC the SetBackgroundStyle behavior change was something that was migrating a bit during the 2.9 series, and the last step of changing the meaning of CUSTOM was changed some time after 2.9.4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.