How to emit events from non-gui object

I figured it out. The problem here is that the PyCommandEvent and
PyEvent have the args in the opposite order!!!
wx.PyEvent.__init__(self, id, evtType)
Vs
Wx.PyCommandEvent.__init__(self, evtType, id)

I had copied the event class from a custom event class based on
PyCommandEvent. The event class I used was:

class LogEvent(wx.PyEvent):
    def __init__(self, evtType, id):
        wx.PyEvent.__init__(self, evtType, id)

That seems very error prone. Especially since you don't get an error,
it just doesn't fire the event as expected.

-Paul

···

-----Original Message-----
From: Lanier, Paul [mailto:Paul.Lanier@analog.com]
Sent: Friday, March 31, 2006 11:35 AM
To: wxPython-users@lists.wxwidgets.org
Subject: RE: [wxPython-users] How to emit events from non-gui object

Robin,
If logger is an instance of Logger and I have the line:
logger.Bind(Logger.EVT_LOG, self.OnLogChange) In another object (not a
Logger object). Will that not cause the event to be processed by
self.OnLogChange in the other object? Below is how I thought this would
work. Maybe you can tell me where my understanding is wrong.

# inside the LogGUIWindow __init__
#1) create a Logger instance
self.logger = Logger()
#2) this adds an entry to the logger's event table for EVT_LOG pointing
to self.OnLogChange self.logger.Bind(Logger.EVT_LOG, self.OnLogChange)

#inside Logger.logWrite
#3) create a log event object
evt = LogEvent(myEVT_LOG, self.id)
#4) set the event object to this logger
evt.SetEventObject(self)
#5) call self.ProcessEvent - This will find the entry in the event table
added by (2) above??
self.ProcessEvent(evt)

-Paul

-----Original Message-----
From: Robin Dunn [mailto:robin@alldunn.com]
Sent: Thursday, March 30, 2006 6:41 PM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] How to emit events from non-gui object

Lanier, Paul wrote:

Hey,

Below is part of a class I wrote for logging user actions in my app.
I was trying to allow the log gui to get an event whenever the log
changed. TO do this I derived from wx.EvtHandler and then proceeded
much the same as I would for a control with a custom event. I can't
get it to work. The Log gui never gets the event. Is this the wrong
way to go about this?

The log gui uses the line below to bind the event:
logger.Bind(Logger.EVT_LOG, self.OnLogChange)

The rest is a portion of my Logger class and it's event class:

myEVT_LOG = wx.NewEventType()
EVT_LOG = wx.PyEventBinder(myEVT_LOG, 1)

class LogEvent(wx.PyEvent):
    def __init__(self, evtType, id):
        wx.PyEvent.__init__(self, evtType, id)

class Logger(wx.EvtHandler):
    def __init__(self):
        wx.EvtHandler.__init__(self)
        self.log =
        self.id = wx.NewId()

    def GetId(self):
        return self.id

    def logWrite(self, device, interface, field, value):

self.log.append(('write',time.time(),device,interface,field,value))
        evt = LogEvent(myEVT_LOG, self.id)
        evt.SetEventObject(self)
        self.ProcessEvent(evt)

Where do you want the event to go at this point? As you have it written
the event will only be sent to self, so you need to do a self.Bind to
connect a handler for it. If you are wanting the event to be sent to
some other object then you need to do otherObject.ProcessEvent(evt).

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Aha, that solves a problem I was having last night, too. Thanks!

I agree that the argument order should be consistent. (It would also be nice to have *Color/*Colour aliases everywhere, or nowhere -- either way if it's consistent! :slight_smile:

-M

···

On Fri, 31 Mar 2006, Lanier, Paul wrote:

I figured it out. The problem here is that the PyCommandEvent and
PyEvent have the args in the opposite order!!!
wx.PyEvent.__init__(self, id, evtType)
Vs
Wx.PyCommandEvent.__init__(self, evtType, id)

I had copied the event class from a custom event class based on
PyCommandEvent. The event class I used was:

class LogEvent(wx.PyEvent):
   def __init__(self, evtType, id):
       wx.PyEvent.__init__(self, evtType, id)

That seems very error prone. Especially since you don't get an error,
it just doesn't fire the event as expected.

-Paul

-----Original Message-----
From: Lanier, Paul [mailto:Paul.Lanier@analog.com]
Sent: Friday, March 31, 2006 11:35 AM
To: wxPython-users@lists.wxwidgets.org
Subject: RE: [wxPython-users] How to emit events from non-gui object

Robin,
If logger is an instance of Logger and I have the line:
logger.Bind(Logger.EVT_LOG, self.OnLogChange) In another object (not a
Logger object). Will that not cause the event to be processed by
self.OnLogChange in the other object? Below is how I thought this would
work. Maybe you can tell me where my understanding is wrong.

# inside the LogGUIWindow __init__
#1) create a Logger instance
self.logger = Logger()
#2) this adds an entry to the logger's event table for EVT_LOG pointing
to self.OnLogChange self.logger.Bind(Logger.EVT_LOG, self.OnLogChange)

#inside Logger.logWrite
#3) create a log event object
evt = LogEvent(myEVT_LOG, self.id)
#4) set the event object to this logger
evt.SetEventObject(self)
#5) call self.ProcessEvent - This will find the entry in the event table
added by (2) above??
self.ProcessEvent(evt)

-Paul

-----Original Message-----
From: Robin Dunn [mailto:robin@alldunn.com]
Sent: Thursday, March 30, 2006 6:41 PM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] How to emit events from non-gui object

Lanier, Paul wrote:

Hey,

Below is part of a class I wrote for logging user actions in my app.
I was trying to allow the log gui to get an event whenever the log
changed. TO do this I derived from wx.EvtHandler and then proceeded
much the same as I would for a control with a custom event. I can't
get it to work. The Log gui never gets the event. Is this the wrong
way to go about this?

The log gui uses the line below to bind the event:
logger.Bind(Logger.EVT_LOG, self.OnLogChange)

The rest is a portion of my Logger class and it's event class:

myEVT_LOG = wx.NewEventType()
EVT_LOG = wx.PyEventBinder(myEVT_LOG, 1)

class LogEvent(wx.PyEvent):
    def __init__(self, evtType, id):
        wx.PyEvent.__init__(self, evtType, id)

class Logger(wx.EvtHandler):
    def __init__(self):
        wx.EvtHandler.__init__(self)
        self.log =
        self.id = wx.NewId()

    def GetId(self):
        return self.id

    def logWrite(self, device, interface, field, value):

self.log.append(('write',time.time(),device,interface,field,value))
        evt = LogEvent(myEVT_LOG, self.id)
        evt.SetEventObject(self)
        self.ProcessEvent(evt)

Where do you want the event to go at this point? As you have it written
the event will only be sent to self, so you need to do a self.Bind to
connect a handler for it. If you are wanting the event to be sent to
some other object then you need to do otherObject.ProcessEvent(evt).

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org