【问题标题】:Python DispatchWithEvents results in attribute error:Python DispatchWithEvents 导致属性错误:
【发布时间】:2018-09-28 18:42:56
【问题描述】:

使用 dispatch as win32com.client.Dispatch 可以正常工作,但会从 DispatchWithEvents 调用 win32com.client.WithEvents 生成 attribute error:,问题一直存在,直到删除 Temp\gen_py 文件夹

我可以在import win32com.client之前删除最开始的Temp\gen_py文件夹

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'

【问题讨论】:

    标签: python events win32com canoe


    【解决方案1】:

    此属性错误的主要原因是您的 COM 服务器已从后期绑定(动态)转换为早期绑定(静态)。

    • 在后期绑定中,每当调用方法时,都会向对象查询该方法,如果成功,则可以进行调用。
    • 在早期绑定中,对象模型的信息是根据对象调用提供的类型信息预先确定的。早期绑定使用 MakePy。此外,早期绑定区分大小写。

    有两种方法可以解决此问题:

    1. 使用动态模块强制您的代码以面向后期绑定的方式工作。使用示例:

      "win32com.client.dynamic.Dispatch()" instead of "win32com.client.Dispatch()" 
      
    2. 在面向早期绑定的方式中使用区分大小写的关键字。使用示例:

      "excel.Visible()" instead of "excel.VISIBLE()" or "excel.visible()"
      

    另外,我认为默认情况下使用早期绑定依赖方法每次都会创建 gen_py 文件夹。

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 2016-11-19
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      相关资源
      最近更新 更多