Problem with custom events (wx.lib.newevent)

The example is broken:

b.bind(wx.EVT_BUTTON, self.on_cmd_click)

should be

        b.Bind(wx.EVT_BUTTON, self.on_cmd_click)

and

self.Bind(EVT_GOO, self.on_cmd1, ID_CMD1)
        self.Bind(EVT_GOO, self.on_cmd2, ID_CMD2)

should be

        self.Bind(EVT_GOO, self.on_cmd1, id=ID_CMD1)
        self.Bind(EVT_GOO, self.on_cmd2, id=ID_CMD2)

but still it doesn’t work. Here is the output from pressing all three buttons from top to bottom:

$ LD_LIBRARY_PATH=/home/thomasc/wxPython_Phoenix-2.9.5.81-r73744-src/build/lib.linux-i686-2.7/wx PYTHONPATH=/home/thomasc/wxPython_Phoenix-2.9.5.81-r73744-src/build/lib.linux-i686-2.7 python x.py

Exception in thread Thread-1:

Traceback (most recent call last):

File “/usr/lib/python2.7/threading.py”, line 552, in __bootstrap_inner

self.run()

File “/usr/lib/python2.7/threading.py”, line 505, in run

self.__target(*self.__args, **self.__kwargs)

File “x.py”, line 14, in evt_thr

wx.PostEvent(win, MooEvent(moo=1))

TypeError: wx._core.Event cannot be instantiated or sub-classed

Traceback (most recent call last):

File “x.py”, line 64, in on_cmd1

self.show("goo = %s" % e.goo, "Got Goo (cmd1)")

AttributeError: ‘CommandEvent’ object has no attribute ‘goo’

Traceback (most recent call last):

File “x.py”, line 67, in on_cmd2

self.show("goo = %s" % e.goo, "Got Goo (cmd2)")

AttributeError: ‘CommandEvent’ object has no attribute ‘goo’

^C

Thomas Christensen wrote:

The example is broken:

         b.bind(wx.EVT_BUTTON, self.on_cmd_click)

should be

         b.Bind(wx.EVT_BUTTON, self.on_cmd_click)

and

         self.Bind(EVT_GOO, self.on_cmd1, ID_CMD1)
         self.Bind(EVT_GOO, self.on_cmd2, ID_CMD2)

should be

         self.Bind(EVT_GOO, self.on_cmd1, id=ID_CMD1)
         self.Bind(EVT_GOO, self.on_cmd2, id=ID_CMD2)

but still it doesn't work. Here is the output from pressing all three
buttons from top to bottom:

$
LD_LIBRARY_PATH=/home/thomasc/wxPython_Phoenix-2.9.5.81-r73744-src/build/lib.linux-i686-2.7/wx
PYTHONPATH=/home/thomasc/wxPython_Phoenix-2.9.5.81-r73744-src/build/lib.linux-i686-2.7
python x.py
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 505, in run
self.__target(*self.__args, **self.__kwargs)
File "x.py", line 14, in evt_thr
wx.PostEvent(win, MooEvent(moo=1))
TypeError: wx._core.Event cannot be instantiated or sub-classed

Traceback (most recent call last):
File "x.py", line 64, in on_cmd1
self.show("goo = %s" % e.goo, "Got Goo (cmd1)")
AttributeError: 'CommandEvent' object has no attribute 'goo'
Traceback (most recent call last):
File "x.py", line 67, in on_cmd2
self.show("goo = %s" % e.goo, "Got Goo (cmd2)")
AttributeError: 'CommandEvent' object has no attribute 'goo'
^C

Which example?

···

--
Robin Dunn
Software Craftsman

Sorry not to be more clear…

The example is from the docstring of the wx.lib.newevent module.

http://wxpython.org/Phoenix/docs/html/lib.newevent.html#module-lib.newevent

I am using custom events heavily and it seems to me that I do not get the GooEvent in which that attributes has been set, but a plain wx.CommandEvent without the attributes.

import wx.lib.newevent as NE

GooEvent, EVT_GOO = NE.NewCommandEvent()

I self.Bind this event and post it and receive it, but the attribute event.foo is not there anymore.

wx.PostEvent(wx.ID_ANY, foo=42)

Also the event generated from NE.NewEvent() complains about subclassing when instantiated.

···

On Monday, April 1, 2013 8:08:17 AM UTC+7, Robin Dunn wrote:

Which example?

Fixed an error in previous message…

wx.PostEvent(wx.ID_ANY, foo=42)

I mean wx.PostEvent(self, GooEvent(wx.ID_ANY, foo=42))

I have attached my modified version of the wx.lib.newevent docstring example which doesn’t behave as expected.

x.py (1.85 KB)

···

On Monday, April 1, 2013 8:08:17 AM UTC+7, Robin Dunn wrote:

Which example?

Thomas Christensen wrote:

    Which example?

Sorry not to be more clear...

The example is from the docstring of the wx.lib.newevent module.

http://wxpython.org/Phoenix/docs/html/lib.newevent.html#module-lib.newevent

I am using custom events heavily and it seems to me that I do not get
the GooEvent in which that attributes has been set, but a plain
wx.CommandEvent without the attributes.

import wx.lib.newevent as NE

GooEvent, EVT_GOO = NE.NewCommandEvent()

I self.Bind this event and post it and receive it, but the attribute
event.foo is not there anymore.

wx.PostEvent(wx.ID_ANY, foo=42)

Also the event generated from NE.NewEvent() complains about subclassing
when instantiated.

Ok, I've got a fix ready that I'll commit later this evening. Unlike most other wx.PyFoo classes the PyEvent and PyCommandEvent classes are still required to be used in Phoenix for custom events, or at least those with custom attributes. This is because wx makes a clone of the event objects and so some extra code is needed to ensure that any Python attributes assigned to the objects are carried over to the clone.

newevent.patch (4.35 KB)

···

On Monday, April 1, 2013 8:08:17 AM UTC+7, Robin Dunn wrote:

--
Robin Dunn
Software Craftsman