How to define event handler in win32com?

Alex Martelli aleaxit at yahoo.com
Mon Jan 22 12:19:36 EST 2001


<shenwang at my-deja.com> wrote in message news:94hfsd$hkd$1 at nnrp1.deja.com...
> After using makepy, I can easily access properties and methods of a com
> object, but I do not how to bind my python function to the events
> defined in that object. Any help?

Using DispatchWithEvents rather than Dispatch is probably simplest,
if you can use it (i.e., if you are creating the object whose
events you need to bind to): you define a class whose methods
have the name of the event-functions you want to be called (when
each suitable event occurs), and pass the class object to
DispatchWithEvents (it's in win32com.client, just like Dispatch).

For a more general solution, you may derive your class from
the makepy-wrapper for the source-interface you're interested
in getting, and instantiate your class with, as its argument,
the specific already-existing object whose events you want to
bind to (remember to call the base-class's __init__ if you
have an __init__ yourself -- THAT is the one that needs the
already-existing object as argument!).


Alex






More information about the Python-list mailing list