【发布时间】:2025-12-07 07:35:02
【问题描述】:
下面的代码可以正常工作。我找不到将一些参数传递给EventHandler 或从EventHandler 调用MainClass 的方法的方法。例如,我不想使用常量param,而是想通过构造函数或setter 方法传递它。我已经尝试过来自here 的建议。但在这种情况下,EventHandler 实例没有捕获任何事件(或者至少没有任何事件出现在标准输出中)。
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()
【问题讨论】:
-
在这里阅读我的答案,这应该可以解决您的问题:*.com/questions/23341675/…
标签: python pywin32 python-multithreading win32com pythoncom