无法使用PyQt4向ActiveX COM对象传递参数

2024-09-30 08:36:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一些Python代码来与thorlabsapt ActiveX控件对话。我的代码基于找到的代码on this page,但尝试使用pyqt4activex容器而不是wxpythonactivex容器。它适用于一个非常简单的ActiveX方法,但是,当我试图调用一个带参数的方法时,我得到了一个错误。在

此代码工作并显示Thorlabs APT的“关于”框:

import sys
from ctypes import *

from PyQt4 import QtGui
from PyQt4 import QAxContainer

class APTSystem(QAxContainer.QAxWidget):

    def __init__(self, parent):
        self.parent = parent
        super(APTSystem, self).__init__()

        self.setControl('{B74DB4BA-8C1E-4570-906E-FF65698D632E}')

        # calling this method works    
        self.AboutBox()

app = QtGui.QApplication(sys.argv)        
a = APTSystem(app)

当我将self.AboutBox()替换为带有参数的方法时,例如:

^{pr2}$

我得到一个错误:TypeError: unable to convert argument 1 of APTSystem.GetNumHWUnitsEx from 'CArgObject' to 'int&'

我假定参数类型需要是ctypes类型。有没有一些ctypes魔法可以解决这个问题?在


Tags: 方法代码fromimportself参数错误sys
1条回答
网友
1楼 · 发布于 2024-09-30 08:36:01

结果我把语法搞错了,通过使用generateDocumentation()函数as mentioned here和一些参数helpfrom here来解决它。工作代码如下:

import sys
from PyQt4 import QtGui
from PyQt4 import QAxContainer
from PyQt4.QtCore import QVariant

class APTSystem(QAxContainer.QAxWidget):

    def __init__(self, parent):

        super(APTSystem, self).__init__()

        # connect to control
        self.setControl('{B74DB4BA-8C1E-4570-906E-FF65698D632E}')

        # required by device
        self.dynamicCall('StartCtrl()')

        # args must be list of QVariants
        typ = QVariant(6)
        num = QVariant(0)
        args = [typ, num]

        self.dynamicCall('GetNumHWUnits(int, int&)', args)

        # only list items are updated, not the original ints!
        if args[1].toInt()[1]:
            print 'Num of HW units =', args[1].toInt()[0]

        self.dynamicCall('StopCtrl()')

app = QtGui.QApplication(sys.argv)        
a = APTSystem(app)

args列表中的第二项包含正确的值,但是num永远不会被调用更新。在

相关问题 更多 >

    热门问题