2
下面的代碼正常工作。我無法找到將某些參數傳遞到EventHandler
或從EventHandler
調用MainClass
的方法的方法。例如,而不是使用常量param
,我想通過構造函數或setter方法傳遞它。我試過here的建議。但在這種情況下,EventHandler
實例不會捕獲任何事件(或者至少在stdout中沒有顯示任何內容)。如何將參數傳遞給win32com事件處理程序
class EventHandler:
param = "value"
def OnConnected(self):
print 'connected'
return True
class MainClass:
def run(self):
pythoncom.CoInitialize()
session = win32com.client.Dispatch("Lib.Obj")
session_id = pythoncom.CoMarshalInterThreadInterfaceInStream(pythoncom.IID_IDispatch, session)
args = { 's_id': session_id, }
thread = threading.Thread(target=self.run_in_thread, kwargs=args)
thread.start()
def run_in_thread(self, s_id):
pythoncom.CoInitialize()
session = win32com.client.DispatchWithEvent(
pythoncom.CoGetInterfaceAndReleaseStream(s_id, pythoncom.IID_IDispatch),
EventHandler
)
session.connect()
while True:
pythoncom.PumpWaitingMessages()
time.sleep(1)
if __name__ == '__main__':
obj = MainClass()
obj.run()
在這裏閱讀我的答案,這應該可以解決您的問題: http://stackoverflow.com/questions/23341675/passing-additional-arguments-to-python-callback-object-win32com-client-dispatch/41140003#41140003 – Vlad