Custom event classes.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom event classes.

Chris Barker - NOAA Federal
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:


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)

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-6-639c0653b5c5> in <module>()

      1 # Create the event

----> 2 evt = SomeNewCommandEvent(attr1="hello", attr2=654)


TypeError: __init__() takes exactly 2 arguments (1 given)


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


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?

I need to make a nice self contained example, but hoped one of you would have some ideas off the top of your head...

And if nothing else, a little more documentation would be good.

-Chris




--

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

[hidden email]

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Custom event classes.

Robin Dunn
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: https://github.com/wxWidgets/Phoenix/blob/master/wx/lib/newevent.py#L133
 

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.

-- 
Robin Dunn
Software Craftsman
http://wxPython.org

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Custom event classes.

Chris Barker - NOAA Federal
On Fri, Feb 2, 2018 at 12:51 PM, Robin Dunn <[hidden email]> 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: https://github.com/wxWidgets/Phoenix/blob/master/wx/lib/newevent.py#L133

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!
 

--

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

[hidden email]

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.