Python and COM events, easy?

logistix logistix at zworg.com
Sun Oct 6 23:20:11 EDT 2002


"Robert Oschler" <Oschler at earthlink.net> wrote in message news:<w8Yn9.20477$Lg2.5488730 at news2.news.adelphia.net>...
> One of the reasons I frequently used Visual Basic when interfacing with COM
> servers is 'events'.  VB made receiving and handling COM events a cakewalk.
> C++ on the other hand, even with ATL 3.0, was still a painful situation.
> 
> Can Python handle COM events, and more importantly, is it easy?  Or do I
> have to go through a lot of hoops when I either create an event sink or have
> to change it.
> 
> If it is easy, then if someone could give a URL to a 'using Python with COM
> and COM events' fast-track doc, I'd appreciate it.
> 
> thx

Open up the source for win32com\client\__init__.py and find the
DispatchWithEvents class.  It's docstring says it all:

  """Create a COM object that can fire events to a user defined class.
  clsid -- The ProgID or CLSID of the object to create.
  user_event_class -- A Python class object that responds to the
events.

  This requires makepy support for the COM object being created.  If
  this support does not exist it will be automatically generated by
  this function.  If the object does not support makepy, a TypeError
  exception will be raised.

  The result is a class instance that both represents the COM object
  and handles events from the COM object.

  It is important to note that the returned instance is not a direct
  instance of the user_event_class, but an instance of a temporary
  class object that derives from three classes:
  * The makepy generated class for the COM object
  * The makepy generated class for the COM events
  * The user_event_class as passed to this function.

  If this is not suitable, see the getevents function for an
alternative
  technique of handling events.

  Object Lifetimes:  Whenever the object returned from this function
is
  cleaned-up by Python, the events will be disconnected from
  the COM object.  This is almost always what should happen,
  but see the documentation for getevents() for more details.

  Example:

  >>> class IEEvents:
  ...    def OnVisible(self, visible):
  ...       print "Visible changed:", visible
  ...
  >>> ie = DispatchWithEvents("InternetExplorer.Application",
IEEvents)
  >>> ie.Visible = 1
  Visible changed: 1
  >>> 
  """



More information about the Python-list mailing list