Custom event classes.

Hi all,

I’m updating a crufty old library that uses old-fashioned event handling (.Connect() and all that). If you’re curious or want to help it’s wxmpl:

https://github.com/NOAA-ORR-ERD/wxmpl

It makes use of custom events, so I’m looking at:

https://wiki.wxpython.org/CustomEventClasses

Those examples seem to work with regular events, but not with CommandEvents:

In [1]: import wx

In [2]: wx.version

Out[2]: ‘4.0.0’

In [3]: import wx.lib.newevent

…:

…: SomeNewEvent, EVT_SOME_NEW_EVENT = wx.lib.newevent.NewEvent()

…: SomeNewCommandEvent, EVT_SOME_NEW_COMMAND_EVENT = wx.lib.newevent.NewCommandEvent()

…:

now this works:

In [5]: # Create the event

…: evt = SomeNewEvent(attr1=“hello”, attr2=654)

but…

In [6]: # Create the event

…: evt = SomeNewCommandEvent(attr1=“hello”, attr2=654)

···

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

So Command events need something else to create them – an ID? what?

Yes, it’s expecting an ID. See the implementation here: Phoenix/wx/lib/newevent.py at master · wxWidgets/Phoenix · GitHub

this does work:

In [7]: # Create the event

…: evt = SomeNewCommandEvent(456, attr1=“hello”, attr2=654)

In [8]: evt

Out[8]: <wx.lib.newevent._Event at 0x102bd0b98>

In [9]: evt.Id

Out[9]: 456

And seems to set the ID of the event itself.

But when I actually tried to use that in code, and with Bind and all that, no such luck. Also – what do I want to use as that ID? is that the idea of the Window it was raised from? or a unique id? (if I use wx.ID_ANY, I get -1 – is that a problem?

Yes, the ID should be an indicator of the object that the event is triggered from. For example, EVT_BUTTON uses the ID of the button widget. EVT_MENU uses the ID of the menu item, etc.

In most cases the ID is used implicitly when binding a handler. For example:

self.Bind(wx.EVT_BUTTON, self.OnButton, someButton)

Will internally call someButton.GetId() to pass to the Connect method.

wx.ID_ANY in the binding is okay because it’s like a wildcard matching the event coming from any source object, but the ID assigned to the event object should not be wx.ID_ANY. That potentially could cause issues when processing the event.

···

On Friday, February 2, 2018 at 11:47:17 AM UTC-8, Chris Barker wrote:

Robin Dunn

Software Craftsman

AARRGG! I could have sword I tried that yesterday, and I could NOT get it
to work.

But I just tried again, and it's working fine -- and a lot less code.

Thanks!

···

On Fri, Feb 2, 2018 at 12:51 PM, Robin Dunn <robin@alldunn.com> wrote:

On Friday, February 2, 2018 at 11:47:17 AM UTC-8, Chris Barker wrote:

So Command events need something else to create them -- an ID? what?

Yes, it's expecting an ID. See the implementation here:
Phoenix/wx/lib/newevent.py at master · wxWidgets/Phoenix · GitHub

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov