无法使用PyQt4将参数传递给ActiveX COM对象 [英] Cannot pass arguments to ActiveX COM object using PyQt4

查看:706
本文介绍了无法使用PyQt4将参数传递给ActiveX COM对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一些Python代码来与Thorlabs APT ActiveX控件交谈。我将代码基于本页上的代码,但试图使用PyQt4 ActiveX容器而不是wxPython ActiveX容器。它适用于一个非常简单的ActiveX方法,但是,当尝试调用一个接受参数的方法时,我收到一个错误。



此代码工作原理并显示Thorlabs APT:

  import sys 
来自ctypes import *

来自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}')


self.AboutBox()

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

当我用一个方法替换 self.AboutBox(),例如:

  num_units = c_int()
self.GetNumHWUnitsEx(21,byref(num_units))
pre>

我得到一个错误: TypeError:无法将APTSystem.GetNumHWUnitsEx的参数1从'CArgObject'转换为'int&'



我认为参数类型需要是ctypes类型。有没有一些ctypes魔法可以解决这个?

解决方案

原来我的语法错误,使用 generateDocumentation / code>函数如此处所述,以及一些参数help 从这里。工作代码如下:

  import从$ PyQt4导入
import QtGui
从PyQt4 import QAxContainer
from PyQt4.QtCore import QVariant

class APTSystem(QAxContainer.QAxWidget):

def __init __(self,parent):

super APTSystem,self).__ init __()

#连接到控制
self.setControl('{B74DB4BA-8C1E-4570-906E-FF65698D632E}')

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

#args必须是QVariants的列表
typ = QVariant(6)
num = QVariant (0)
args = [typ,num]

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

更新,而不是原来的ints!
if args [1] .toInt()[1]:
print'Num of HW units =',args [1] .toInt()[0]

.dynamicCall('StopCtrl()')

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

args 列表中的第二个项目包含正确的值,但 num 不会被呼叫更新。


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.

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)

When I replace self.AboutBox() with a method with arguments e.g.:

num_units = c_int()
self.GetNumHWUnitsEx(21, byref(num_units))

I get an error: TypeError: unable to convert argument 1 of APTSystem.GetNumHWUnitsEx from 'CArgObject' to 'int&'

I presume the argument type needs to be a ctypes type. Is there some ctypes magic that can solve this?

解决方案

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)

The second item in the args list contains the correct value, but num is never updated by the call.

这篇关于无法使用PyQt4将参数传递给ActiveX COM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆