Why does event.GetEventObject() return None if the event is fired by calling wx.EvtHandler.ProcessEvent()?

Here's an example (Suse SLES10, wxpython 2.8.12.1, and python 2.7.2).
We use ProcessEvent to create a mock event in testing but then we
cannot intercept the object associated.

···

=========================================================================
import wx

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'CheckBox Example',
size=(200, 200))
        self.checkbox = wx.CheckBox(self, -1, "Click Me.", pos=(20,
110))
        self.Bind(wx.EVT_CHECKBOX, self.OnCheckBox, self.checkbox)

    def OnCheckBox(self, event):
        print 'Event ojbect:', event.GetEventObject()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = TestFrame()
    frame.Show()
    event = wx.CommandEvent(wx.wxEVT_COMMAND_CHECKBOX_CLICKED,
frame.checkbox.GetId())
    frame.GetEventHandler().ProcessEvent(event)
    app.MainLoop()

The screen output reads.
Event ojbect: None
Event ojbect: <wx._controls.CheckBox; proxy of <Swig Object of type
'wxCheckBox *' at 0xad2490> >
Event ojbect: <wx._controls.CheckBox; proxy of <Swig Object of type
'wxCheckBox *' at 0xad2490> >

Hi

···

On Jul 3, 2012 4:25 PM, “JR” jhrhew@hotmail.com wrote:

Here’s an example (Suse SLES10, wxpython 2.8.12.1, and python 2.7.2).
We use ProcessEvent to create a mock event in testing but then we
cannot intercept the object associated.

=========================================================================
import wx

class TestFrame(wx.Frame):
def init(self):
wx.Frame.init(self, None, -1, ‘CheckBox Example’,
size=(200, 200))
self.checkbox = wx.CheckBox(self, -1, “Click Me.”, pos=(20,
110))
self.Bind(wx.EVT_CHECKBOX, self.OnCheckBox, self.checkbox)

def OnCheckBox(self, event):
    print 'Event ojbect:', event.GetEventObject()

if name == ‘main’:
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
event = wx.CommandEvent(wx.wxEVT_COMMAND_CHECKBOX_CLICKED,
frame.checkbox.GetId())

Because you never set the event object it defaults to none.

Cody

Thanks a lot for the solution.

However, I don’t understand why you need to set the event object even though you provided the id of the object to ProcessEvent.

···

On Tuesday, July 3, 2012 2:28:55 PM UTC-7, Cody Precord wrote:

Hi

On Jul 3, 2012 4:25 PM, “JR” jhrhew@hotmail.com wrote:

Here’s an example (Suse SLES10, wxpython 2.8.12.1, and python 2.7.2).
We use ProcessEvent to create a mock event in testing but then we
cannot intercept the object associated.

=========================================================================
import wx

class TestFrame(wx.Frame):
def init(self):
wx.Frame.init(self, None, -1, ‘CheckBox Example’,
size=(200, 200))
self.checkbox = wx.CheckBox(self, -1, “Click Me.”, pos=(20,
110))
self.Bind(wx.EVT_CHECKBOX, self.OnCheckBox, self.checkbox)

def OnCheckBox(self, event):
    print 'Event ojbect:', event.GetEventObject()

if name == ‘main’:
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
event = wx.CommandEvent(wx.wxEVT_COMMAND_CHECKBOX_CLICKED,
frame.checkbox.GetId())

Because you never set the event object it defaults to none.

Cody

JR wrote:

Thanks a lot for the solution.
However, I don't understand why you need to set the event object even
though you provided the id of the object to ProcessEvent.

I suppose there's really no answer other than "because you do". I've
been trying to come up with a circumstance where the event object might
be different from the one in the constructor, but I have failed. One
might invoke the old Python rule "explicit is better than implicit", but
in this case the design comes from the C++ wxWidgets library.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

In EVT_MENU events the ID is the menu item's ID but the event object is something else depending on where the event came from.

···

On 7/3/12 2:52 PM, Tim Roberts wrote:

JR wrote:

Thanks a lot for the solution.
However, I don't understand why you need to set the event object even
though you provided the id of the object to ProcessEvent.

I suppose there's really no answer other than "because you do". I've
been trying to come up with a circumstance where the event object might
be different from the one in the constructor, but I have failed. One
might invoke the old Python rule "explicit is better than implicit", but
in this case the design comes from the C++ wxWidgets library.

--
Robin Dunn
Software Craftsman