python调用有事件发生的com时,需要一些技巧。
我们以ie这个com为例来讲解一下我的经验。
首先我们需要pywin32这个模块的支持,它提供了我们调用com便利直接方法。你可以www.sf.net搜索并下载它。
先运行如下代码:
| | | import win32gui | | import win32com | | import win32com.client | | import pythoncom | | import time | | | | class test: | | def runtest(self): | | print 'tuntest' | | | | class EventHandler: | | | | def OnVisible(self, visible): | | global bVisibleEventFired | | bVisibleEventFired = 1 | | def OnDownloadBegin(self): | | print "DownloadBegin" | | def OnDownloadComplete(self): | | print "DownloadComplete" | | def OnDocumentComplete(self, pDisp = pythoncom.Missing , URL = pythoncom.Missing): | | print "documentComplete of %s" % URL | | | | self.runtest() | | | | class runcom(test): | | def __init__(self): | | ie = win32com.client.DispatchWithEvents("InternetExplorer.Application", EventHandler) | | ie.Visible = 1 | | ie.Navigate("www.aawns.com") | | | | self.runtest() | | pythoncom.PumpMessages() | | ie.Quit() | | a=runcom()COPY |
我们看到,程序运行正常,能打开我们指定的站点,并各事件被触发后都能作出正确的反映。
但是假如我们希望在事件发生后,能调用我们继承下来的一些方法和属性。你会发现无法使用。
如下代码将展示这个例子
| | | import win32gui | | import win32com | | import win32com.client | | import pythoncom | | import time | | | | class test: | | def runtest(self): | | print 'tuntest' | | | | class EventHandler: | | | | def OnVisible(self, visible): | | global bVisibleEventFired | | bVisibleEventFired = 1 | | def OnDownloadBegin(self): | | print "DownloadBegin" | | def OnDownloadComplete(self): | | print "DownloadComplete" | | def OnDocumentComplete(self, pDisp = pythoncom.Missing , URL = pythoncom.Missing): | | print "documentComplete of %s" % URL | | | | self.runtest() | | | | class runcom(test): | | def __init__(self): | | ie = win32com.client.DispatchWithEvents("InternetExplorer.Application", EventHandler) | | ie.Visible = 1 | | ie.Navigate("www.aawns.com") | | | | self.runtest() | | pythoncom.PumpMessages() | | ie.Quit() | | a=runcom()COPY |
运行结果是错误的。
tuntest
DownloadBegin
DownloadComplete
DownloadBegin
DownloadComplete
documentComplete of http://www.aawns.com/
pythoncom error: Python error invoking COM method.
Traceback (most recent call last):
File "C:\Python23\Lib\site-packages\win32com\server\policy.py", line 283, in _
Invoke_
return self._invoke_(dispid, lcid, wFlags, args)
File "C:\Python23\Lib\site-packages\win32com\server\policy.py", line 288, in _
invoke_
return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None)
File "C:\Python23\Lib\site-packages\win32com\server\policy.py", line 550, in _
invokeex_
return func(*args)
File "D:\python\test.py", line 24, in OnDocumentComplete
self.runtest()
File "C:\Python23\Lib\site-packages\win32com\client\__init__.py", line 454, in
__getattr__
raise AttributeError, "'%s' object has no attribute '%s'" % (repr(self), att
r)
exceptions.AttributeError: '' object has no attribute 'runtest'
我们看到test类的runtest方法并没有被继承进去,为什么呢?我的理解是因为com的事件模式让你无法继承python中的self,因为在调用 ie的时候并不是用EventHandler的实例而是将这个类作为了事件处理的方法(不知道这里理解是否正确,如果有更好的理解。我们交流)
经过查找了很多资料和试探了很多方法,只有采用全局变量的方式才能在事件和各个类之间传递数据。代码变更成了这样
| | | import win32gui | | import win32com | | import win32com.client | | import pythoncom | | import time | | | | | | class EventHandler: | | | | def OnVisible(self, visible): | | global bVisibleEventFired | | bVisibleEventFired = 1 | | def OnDownloadBegin(self): | | print "DownloadBegin" | | | | global testlist | | testlist.append("DownloadBegin") | | def OnDownloadComplete(self): | | print "DownloadComplete" | | | | global testlist | | testlist.append("DownloadComplete") | | def OnDocumentComplete(self, pDisp = pythoncom.Missing , URL = pythoncom.Missing): | | print "documentComplete of %s" % URL | | | | global testlist | | print testlist | | | | class runcom: | | def __init__(self): | | global testlist | | ie = win32com.client.DispatchWithEvents("InternetExplorer.Application", EventHandler) | | ie.Visible = 1 | | ie.Navigate("www.aawns.com") | | | | print testlist | | pythoncom.PumpMessages() | | ie.Quit() | | testlist=[] | | a=runcom()COPY |
可以看到,用全局变量的方式解决了于事件内传输数据的问题。
没有解决的问题:使用Twisted的时候也有相应的事件,如何保证Twisted 和com中的事件都被触发?
后来经过拐拐龙底咚朋友的提醒,知道了可以通过继承的方式将类的事件和属性继承下来。例子代码:
| import win32gui | | import win32com | | import win32com.client | | import pythoncom | | import time | | | | class Test: | | def runtest(self): | | print 'test' | | | | class EventHandler: | | def OnVisible(self,visible): | | global bVisibleEventFired | | bVisibleEventFired = 1 | | def OnDownloadBegin(self): | | print 'DownloadBegin' | | self.runtest() | | self.value = 1 | | def OnDownloadComplete(self): | | print 'DownloadComplete' | | self.value += 1 | | def OnDocumentComplete(self,pDisp=pythoncom.Missing,URL=pythoncom.Missing): | | print 'documentComplete of %s' %URL | | print self.value | | | | class H(Test,EventHandler): | | pass | | | | ie = win32com.client.DispatchWithEvents('InternetExplorer.Application',H) | | ie.Visible = 1 | | ie.Navigate("www.sohu.com") | | pythoncom.PumpMessages() | | ie.Quit()COPY |
原文:http://bbs.chinaunix.net/thread-558195-1-1.html |