Om *_*ary 5 python events win32com canoe
使用 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
Run Code Online (Sandbox Code Playgroud)
对于我的测试中的第一次迭代工作正常,但对于相同代码的第二次迭代会产生 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()
Run Code Online (Sandbox Code Playgroud)
AttributeError: 对象没有属性
AttributeError: '<win32com.gen_py.CANoe 8.5 Type Library.ITestSetupItem instance at 0x49756368>' object has no attribute 'TestVariant'
Run Code Online (Sandbox Code Playgroud)
小智 2
此属性错误的主要原因是您的 COM 服务器已从后期绑定(动态)转变为早期绑定(静态)。
有两种方法可以解决此问题:
使用动态模块强制您的代码以面向后期绑定的方式工作。使用示例:
"win32com.client.dynamic.Dispatch()" instead of "win32com.client.Dispatch()"
Run Code Online (Sandbox Code Playgroud)使用驼峰敏感关键字进行早期绑定导向方式。使用示例:
"excel.Visible()" instead of "excel.VISIBLE()" or "excel.visible()"
Run Code Online (Sandbox Code Playgroud)另外,我认为默认情况下使用早期绑定相关方法每次都会创建 gen_py 文件夹。
| 归档时间: |
|
| 查看次数: |
1008 次 |
| 最近记录: |