无法使用 PyQt4 将参数传递给 ActiveX COM 对象
问题描述
我正在尝试编写一些 Python 代码来与 Thorlabs APT ActiveX 控件通信.我的代码基于 在此页面,但尝试使用 PyQt4 ActiveX 容器而不是 wxPython ActiveX 容器.它适用于非常简单的 ActiveX 方法,但是在尝试调用带参数的方法时出现错误.
I'm trying to write some Python code to talk to the Thorlabs APT ActiveX control. I'm basing my code on the code found on this page, but trying to use PyQt4 ActiveX container instead of the wxPython ActiveX container. It works for a very simple ActiveX methods, however, I get an error when trying to call a method that takes arguments.
此代码有效并显示了 Thorlabs APT 的关于框:
This code works and shows the about box for 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()
时,例如:
When I replace self.AboutBox()
with a method with arguments e.g.:
num_units = c_int()
self.GetNumHWUnitsEx(21, byref(num_units))
我收到一个错误:TypeError: unable to convert argument 1 of APTSystem.GetNumHWUnitsEx from 'CArgObject' to 'int&'
我认为参数类型需要是 ctypes 类型.是否有一些 ctypes 魔法可以解决这个问题?
I presume the argument type needs to be a ctypes type. Is there some ctypes magic that can solve this?
解决方案
原来我的语法完全错误,通过使用 generateDocumentation()
函数 这里提到,还有一些参数帮助从这里.工作代码如下所示:
Turns out I had the syntax quite wrong, worked it out by using the generateDocumentation()
function as mentioned here, and some parameter help from here. The working code looks like:
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
永远不会被调用更新.
The second item in the args
list contains the correct value, but num
is never updated by the call.
相关文章