Calling an event handler

David: Thanks for the reply. I'll stick with 1 for now but keep your
other approach in mind for when I may need it.

I guess what I was in part really fishing for was how to programatically
emit an event (like raising an exception): some way to whap a wxWindows
object up side the head and make it emit an event. For now this is mostly
just curiosity, and it's not obvious to me that such a capability gets you
anything that you otherwise couldn't do. But if it's possible to do it,
I'd like to know.

···

--------------------------------------
Gary H. Merrill
Director and Principal Scientist, New Applications
Data Exploration Sciences
GlaxoSmithKline Inc.
(919) 483-8456

Create your own event class like this

wxEVT_WRITE_LOG = wxNewEventType()

def EVT_WRITE_LOG(win, func):
     win.Connect(-1, -1, wxEVT_WRITE_LOG, func)

class WriteLogEvent(wxPyEvent):
     def __init__(self, message):
         wxPyEvent.__init__(self)
         self.SetEventType(wxEVT_WRITE_LOG)
         self.message = message

Then register this event with some window

EVT_WRITE_LOG(self, self.onWriteLog)

and write a handler for it, for example

def onWriteLog(self, event):
     '''Action on an event to write a message to a log window.'''

     self.displayMessage(event.message)

Then, when you need it, create an event object and post it to the window where it's registered

  evt = WriteLogEvent(message)
  wxPostEvent(self.__topframe, evt)

Any existing wx event can be created and posted this way

Marina Gourtovaia
Research Associate
Engineering Design Center
Department of Engineering
University of Cambridge
Cambridge, UK

Tel: +44 (0)1223 766961
Fax: +44 (0)1223 332662

···

gary.h.merrill@gsk.com wrote:

David: Thanks for the reply. I'll stick with 1 for now but keep your
other approach in mind for when I may need it.

I guess what I was in part really fishing for was how to programatically
emit an event (like raising an exception): some way to whap a wxWindows
object up side the head and make it emit an event. For now this is mostly
just curiosity, and it's not obvious to me that such a capability gets you
anything that you otherwise couldn't do. But if it's possible to do it,
I'd like to know.

David: Thanks for the reply. I'll stick with 1 for now but keep
your other approach in mind for when I may need it.

I guess what I was in part really fishing for was how to
programatically emit an event (like raising an exception): some way
to whap a wxWindows object up side the head and make it emit an
event. For now this is mostly just curiosity, and it's not obvious
to me that such a capability gets you anything that you otherwise
couldn't do. But if it's possible to do it, I'd like to know.

Seems like that ability might come in handy for adding a "macro" feature
to our apps and/or writing automated regression tests! I haven't
explored either topic yet.

There are names like wxPostEvent, AddPendingEvent, etc. that you can
search for in the docs, wiki and mailing list for more info.

···

On Wednesday 30 April 2003 07:06 am, gary.h.merrill@gsk.com wrote:

On Wednesday 30 April 2003 08:54 am, Marina Gourtovaia wrote:

Then, when you need it, create an event object and post it to the
window where it's registered

  evt = WriteLogEvent(message)
  wxPostEvent(self.__topframe, evt)

Any existing wx event can be created and posted this way

Right. Although "9 times out of 10", when you make a _custom_ event, you
could more easily just write:

    wxCallAfter(self.writeLog, message)

which uses wxPostEvent() under the hood. wxCallAfter() doesn't require
any special subclassing, declarations or bindings.

--
Chuck
http://ChuckEsterbrook.com