Cannot pass arguments to ActiveX COM object using PyQt4(无法使用 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.
这篇关于无法使用 PyQt4 将参数传递给 ActiveX COM 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法使用 PyQt4 将参数传递给 ActiveX COM 对象
基础教程推荐
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
