0
votes

I am having trouble posting an erase background event to draw to the screen. In my full code, I want to draw a bitmap (DC.DrawBitmap()) when a button is clicked. I do that by posting an EVT_ERASE_BACKGROUND event which is caught by a custom bound method. However, once it is in that method, the event.GetDC() method that normally works fails.

Here is a simplified code that has the same result:

import wx

class Foo(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__ (self, parent, -1, title, size=(500,300))
        self.panel = wx.Panel(self, -1)

        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        self.Bind(wx.EVT_ENTER_WINDOW, self.onEnter)

        self.Show()

    def OnEraseBackground(self, e):
        DC = e.GetDC()

    def onEnter(self, e):
        wx.PostEvent(self, wx.PyCommandEvent(wx.wxEVT_ERASE_BACKGROUND))

app = wx.App()
Foo(None, 'foo')
app.MainLoop()

This raises:

AttributeError: 'PyCommandEvent' object has no attribute 'GetDC'

How can I solve this?

1

1 Answers

0
votes

Worked on it for an hour without success before posting, then solved it myself five minutes later...

Here's my solution, creating a ClientDC if the event doesn't have its own DC:

import wx

class Foo(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__ (self, parent, -1, title, size=(500,300))
        self.panel = wx.Panel(self, -1)

        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        self.Bind(wx.EVT_ENTER_WINDOW, self.onEnter)

        self.Show()

    def OnEraseBackground(self, e):
        try:
            DC = e.GetDC()
        except:
            DC = wx.ClientDC(self)
        DC.Clear()

    def onEnter(self, e):
        wx.PostEvent(self, wx.PyCommandEvent(wx.wxEVT_ERASE_BACKGROUND))

app = wx.App()
Foo(None, 'foo')
app.MainLoop()