我有一个使用win32com库来控制iTunes程序,但一直有一些问题得到它编译成可执行文件。这个问题似乎围绕着使用DispatchWithEvents
代替Dispatch
。我创建了一个非常简单的程序来说明我的问题:py2exe/pyinstaller和DispatchWithEvents
import win32com.client
win32com.client.gencache.is_readonly = False #From py2exe wiki
class ITunesEvents(object):
def __init__(self): self.comEnabled = True
def OnCOMCallsDisabledEvent(self, reason): self.comEnabled = False
def OnCOMCallsEnabledEvent(self): self.comEnabled = True
# The first line works in the exe, the second doesn't.
itunes = win32com.client.Dispatch("iTunes.Application")
#itunes = win32com.client.DispatchWithEvents("iTunes.Application", ITunesEvents)
lib = getattr(itunes, "LibraryPlaylist")
src = getattr(lib, "Source")
playlists = getattr(src, "Playlists")
print "Found %i playlists." % getattr(playlists, "Count")
使用Dispatch
,程序编译和运行正常。使用DispatchWithEvents
,从命令行调用的时候,程序运行良好,但生产运行exe文件时出现以下错误:
Traceback (most recent call last):
File "sandbox.py", line 16, in <module>
itunes = win32com.client.DispatchWithEvents("iTunes.Application", ITunesEvents)
File "win32com\client\__init__.pyc", line 252, in DispatchWithEvents
File "win32com\client\gencache.pyc", line 520, in EnsureModule
File "win32com\client\gencache.pyc", line 287, in MakeModuleForTypelib
File "win32com\client\makepy.pyc", line 259, in GenerateFromTypeLibSpec
File "win32com\client\gencache.pyc", line 141, in GetGeneratePath
IOError: [Errno 2] No such file or directory: '[distDir]\\library.zip\\win32com\\gen_py\\__init__.py'
我使用PyInstaller,这给出了一个类似的错误也试过:
File "<string>", line 16, in <module>
File "[outDir]/win32com.client", line 252, in DispatchWithEvents
File "[outDir]/win32com.client.gencache", line 520, in EnsureModule
File "[outDir]/win32com.client.gencache", line 287, in MakeModuleForTypelib
File "[outDir]/win32com.client.makepy", line 286, in GenerateFromTypeLibSpec
File "[outDir]/win32com.client.gencache", line 550, in AddModuleToCache
File "[outDir]/win32com.client.gencache", line 629, in _GetModule
File "[pyinstallerDir]\iu.py", line 455, in importHook
raise ImportError, "No module named %s" % fqname
ImportError: No module named win32com.gen_py.9E93C96F-CF0D-43F6-8BA8-B807A3370712x0x1x13
我知道我可以手动添加类型库在我setup.py
文件,但我想不用重新编译,所以我宁愿动态地创建与运行不同版本的iTunes计算机上的代码。如果没有办法通过setup/spec来做到这一点,也许有另一种方法来加载事件?谢谢。
增加:
感谢瑞安,我发现我可以把生成的PY文件和一个小挖之后,能够拿出以下。
将生成的py文件(来自makepy.py
)并将其重命名为cominterface.py
。然后,您需要执行以下操作以实际创建具有事件处理程序的COM对象。
import cominterface
from types import ClassType
from win32com.client import EventsProxy, _event_setattr_
class ItunesEvents:
'''iTunes events class. See cominterface for details.'''
def OnPlayerPlayEvent(self, t):print "Playing..."
def OnPlayerStopEvent(self, t): print "Stopping..."
itunes = cominterface.iTunesApp()
rClass = ClassType("COMEventClass", (itunes.__class__, itunes.default_source, ItunesEvents), {'__setattr__': _event_setattr_})
instance = rClass(itunes._oleobj_)
itunes.default_source.__init__(instance, instance)
#ItunesEvents.__init__(instance) #Uncomment this line if your events class has __init__.
itunes = EventsProxy(instance)
然后你可以去做你的生意。
I我认为这可行,但我似乎无法弄清楚如何明确地调用接口,特别是使用事件。我有''Dispatch''工作,但后来意识到这只是调用win32com正常。有没有关于如何做到这一点的信息? – jeffaudio 2010-04-26 17:14:29