[python-win32] accessing application object from
OnConnectionmethod in another Class
Mark Hammond
mhammond at skippinet.com.au
Fri Oct 1 00:32:55 CEST 2004
> If you look at the example demo "ExcelAddin" that come with win32all,
FYI,
http://cvs.sourceforge.net/viewcvs.py/spambayes/spambayes/Outlook2000/addin.
py is a complete Office Addin, and has jumped through many hoops.
>
> def OnConnection(self, application, connectMode, addin, custom):
> self.appHostApp = application
> btnMyButton = self.toolbarButton =
> DispatchWithEvents(btnMyButton, ButtonEvent)
Just ignore the man behind the curtain :) Do it the way you would normally
do it!
btnMyButton._application = self
should just store it in an instance variable. Obviously you will want to
avoid conflict with Word's attribute names - we first try and set the
attribute to word, and it that fails, store it in the instance.
Another alterative is to just add your own method to the event class:
class ButtonEvent:
# as normal
def _MyFunkyMethod(self, whatever):
...
btnMyButton = DispatchWithEvents(btnMyButton, ButtonEvent)
btnMyButton._MyFunkyMethod(self)
Note that you need to be very careful with DispatchWithEvents and circular
references. These DWI objects have a reference to the original object (eg,
the Excel object the events are being sourced from), and the original object
has a reference to the DWI object (as that is where it is delivering
events).
Eg,
a = Dispatch("Word.App")
e = DispatchWithEvents(a, myclass)
'e' has a reference to 'a', and 'a' has a reference to 'e' - this is a
circular reference that must be manually broken.
However:
a = Dispatch("Word.App")
e = WithEvents(a)
Avoids that circle - 'a' still holds a reference to 'e', but 'e' holds no
reference to a. Thus, as soon as a or e go out of scope, it all still
destructs correctly.
That is one hoop SpamBayes had to jump through to stop certain events
leaking.
> to name a few. I have also spent some time in the MSDN and Google and
> still am confused. Have ordered Marks book "Python programming on
> win32" but it will only be here in 10 days (i count the minutes). so
Mark.
More information about the Python-win32
mailing list