【问题标题】:How to pass enum as argument in ctypes python?如何在 ctypes python 中将枚举作为参数传递?
【发布时间】:2016-07-13 16:06:41
【问题描述】:

我有一个dll 和一个c 头文件.h。有一些函数接受cenum,例如;

enum Type
{
    typeA,
    typeB
};

还有一个函数

bool printType(Type type );

现在我想从 python 脚本中使用dll 的这个函数。我已经导入了库。这是成功的。我可以访问其他功能并且它可以工作。所以有一个像我上面给出的例子一样的函数,它以Enum 作为参数。

查看此处的其他帖子和互联网上的其他内容后,我尝试了什么;

# -*- coding: utf-8 -*-
from ctypes import *
from enum import IntEnum

class CtypesEnum(IntEnum):
    @classmethod
    def from_param(cls, obj):
        return int(obj)

class Type(CtypesEnum):
    typeA     = 0
    typeB     = 1

我是这样使用的。

setEnumtype = self.printType
setEnumtype.argtypes = [Type]
setEnumVale.restype = c.c_int
result = self.printType(setEnumtype.typeA)

解释器说,它知道IntEnum。我已经通过 pip 安装了enum

还有其他直接的方法吗?

追溯:

from enum import IntEnum
ImportError: cannot Import Name IntEnum

【问题讨论】:

  • 请发布 exact 回溯(即复制并粘贴文本)
  • 我已经发布了原始回溯。

标签: python enums ctypes


【解决方案1】:

如果您阅读the enum docs on pip,您会看到:

Python 3 现在在其标准库中具有一个取代此库的枚举实现(也可用于较旧的 Python 版本,作为第三方flufl.enum 发行版)。

你实际上想要enum34

【讨论】:

  • attributeError: Instancmethod 对象没有属性类型argtypes
  • @GulluButt 您可能想用产生此错误的代码更新您的问题
猜你喜欢
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
  • 2013-09-28
  • 1970-01-01
  • 2011-07-18
相关资源
最近更新 更多