python - 用 Cython 包装包含 wxString 的 C++ 类

标签 python wxpython wxwidgets cython

我正在开发一个 Python 扩展,以配合使用 wxWidgets 编写的 C++ 应用程序的 GUI。我正在使用 Cython,并且有基本系统(构建工具,加上具有适当版本详细信息的入门扩展等)愉快地工作。

我只对提供后端(非 GUI)功能感兴趣,例如文件解析和处理。但是,所有类——不仅仅是 GUI 类——都使用 wxString 作为字符串数据,例如下面的最小示例:

#include <wx/string.h>

class MyClass{
    wxString name;
    wxString get_name(){
        return this->name;
    }
};

我的问题是包装这样一个类的最佳方法是什么?有没有一种简单的方法来连接 Python 字符串和 wxString 实例?或者我还需要包装 wxString 类吗?我是否能够以某种方式与 wxPython 端口结合以避免重新发明轮子?

最佳答案

我通过使用静态 wxString::FromUTF8() 函数将 Python 转换为 wxString,并使用 wxString.ToUTF8() 进入另一个方向。以下是我想出的代码:

# Import the parts of wxString we want to use.
cdef extern from "wx/string.h":
    cdef cppclass wxString:
        char* ToUTF8()


# Import useful static functions from the class.
cdef extern from "wx/string.h" namespace "wxString":
   wxString FromUTF8(char*)


# Function to convert from Python string to wxString. This can be given either
# a unicode string, or a UTF-8 encoded byte string. Results with other encodings
# are undefined and will probably lead to errors.
cdef inline wxString from_python(python_string):
    # If it is a Python unicode string, encode it to a UTF-8 byte string as this
    # is how we will pass it to wxString.
    if isinstance(python_string, unicode):
        byte_string = python_string.encode('UTF-8')

    # It is already a byte string, and we have no choice but to assume its valid
    # UTF-8 as theres no (sane/efficient) way to detect the encoding.
    else:
        byte_string = python_string

    # Turn the byte string (which is still a Python object) into a C-level char*
    # string.
    cdef char* c_string = byte_string

    # Use the static wxString::FromUTF8() function to get us a wxString.
    return FromUTF8(c_string)


# Function to convert a wxString to a UTF-8 encoded Python byte string.
cdef inline object to_python_utf8(wxString wx_string):
    return wx_string.ToUTF8()


# Function to convert a wxString to a Python unicode string.
cdef inline object to_python_unicode(wxString wx_string):
    # Since the wxString.ToUTF8() method returns a const char*, we'd have to try
    # and cast it if we wanted to do it all in here. I've tried this and can't
    # seem to get it to work. But calling the to_python_utf8() function
    # means Cython handles the conversions and it all just works. Plus, since
    # they are defined as inline functions this may well be simplified down when
    # compiled.
    byte_string = to_python_utf8(wx_string)

    # Decode it to a unicode string and we're done.
    return byte_string.decode('UTF-8')

简单地把它放在一个 .pxd 文件中(就我个人而言,我把它放在一个子目录中作为 wx/string.pxd - 确保你也创建了一个 wx/__init__.pdx 如果你选择这样做)。然后 cimport 它并根据需要调用函数:

cimport wx.string

wx_string = wx.string.from_python(python_string)
python_string = wx.string.to_python_unicode(wx_string)

关于python - 用 Cython 包装包含 wxString 的 C++ 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6370560/

相关文章:

python - 导入仅在一个文件中使用的 python 模块

python - 使用嵌套列表打印 5x5 数字

python - wxpython wx.Notebook 没有获得任何宽度

python - 如何在 Ubuntu 14.10 上的 Python 2.7 virtualenv 中安装 wxPython 3.0.1.1?

python - 无法专注于 OS/X 中的小部件

c++ - 在代码块中找不到 -lwxmsw30u_core 和 -lwxbase30u

c++ - 运行时错误-f1.exe 不存在或不是可执行文件

python - 使用 python 查找 CSV 中的列号

python - 我如何从 _ast.Dict 转换为实际的字典?

python - wxPython - 从另一个类获取属性?