使用 dispatch aswin32com.client.Dispatch
工作正常,但从attribute error:
DispatchWithEvents 调用产生win32com.client.WithEvents
,问题一直存在,直到Temp\gen_py
文件夹被删除
我可以从Temp\gen_py
一开始就删除文件夹import win32com.client
path=r"C:\Users\omc\AppData\Local\Temp\gen_py"
rmtree(path, ignore_errors=True)
while os.path.exists(path):
pass
对于我的测试中的第一次迭代工作正常,但对于相同代码的第二次迭代会产生attribute error:
from shutil import rmtree
path=r"C:\Users\omc\AppData\Local\Temp\gen_py"
rmtree(path, ignore_errors=True)
while os.path.exists(path):
pass
import win32com.client
class CanoeTestModuleEvents(object):
"""Handler for CANoe TestModule events"""
def OnStart(self):
print("< Test Module started >")
bTestModuleRunning = True
def OnStop(self,Reason) :
print("< Test Module stopped >")
bTestModuleRunning = False
if Reason == 0:
print("Test module was executed completely")
else:
if Reason== 1:
print("Test module was stopped by the user")
else:
print("Test module was stopped by measurement stop")
APP = win32com.client.Dispatch("CANoe.Application")
App.load("CANoeApplication.cfg")
# ---------------------------------------------------------------
# TestEnvironment Item(2)
# ---------------------------------------------------------------
TestEnvironment = self.App.Configuration.TestSetup.TestEnvironments.Item(1)
TestModule = self.TestEnvironment.Items.Item(2)
TestModule.TestVariant = TestVariant
App.Measurement.Start()
WaitForMeasurementStart()
win32com.client.WithEvents(TestModule, CanoeTestModuleEvents)
if MeasurementRunning():
TestModule.Start()
WaitForTestModuleStart()
while app.bTestModuleRunning == True:
pythoncom.PumpWaitingMessages()
time.sleep(.1)
App.Measurement.Stop()
# ---------------------------------------------------------------
# TestEnvironment Item(3)
# ---------------------------------------------------------------
TestEnvironment = self.App.Configuration.TestSetup.TestEnvironments.Item(1)
TestModule = self.TestEnvironment.Items.Item(3)
TestModule.TestVariant = TestVariant
App.Measurement.Start()
WaitForMeasurementStart()
win32com.client.WithEvents(TestModule, CanoeTestModuleEvents)
if MeasurementRunning():
TestModule.Start()
WaitForTestModuleStart()
while app.bTestModuleRunning == True:
pythoncom.PumpWaitingMessages()
time.sleep(.1)
App.Measurement.Stop()
AttributeError:对象没有属性
AttributeError: '<win32com.gen_py.CANoe 8.5 Type Library.ITestSetupItem instance at 0x49756368>' object has no attribute 'TestVariant'