我只对使后端(非GUI)功能可用感兴趣,比如文件解析和处理。但是,所有类(不仅仅是GUI类)都使用wxString
作为字符串数据,例如下面的最小示例:
#include <wx/string.h>
class MyClass{
wxString name;
wxString get_name(){
return this->name;
}
};
我的问题是包装这样一个类的最好方法是什么?在Python字符串和wxString
实例之间有没有一种简单的接口方法?或者我也需要包装wxString
类吗?我能不能在某种程度上与wxPython端口连接以避免重新发明轮子?在
我尝试的第一种方法是使用wxString构造函数:
并将constchar*string传递给它来创建对象。在
然后编写一些内联函数将python字符串转换为wxString,反之亦然。在
^{pr2}$现在我看到的唯一缺点是,在wxString和python世界中,字符串可能是重复的。在
第二种方法是使用Python的PyString对象字符缓冲区,将wxString子类化并手动重新实现所有操作。Cython可以帮助编写这样的子类。在
我通过使用静态
wxString::FromUTF8()
函数将Python转换为wxString,并使用wxString.ToUTF8()
将其转换为另一个方向。下面是我想出的代码:只需将它放在一个.pxd文件中(就个人而言,我将其放在子目录中,作为
^{pr2}$wx/string.pxd
-如果您选择同样的操作,请确保您还创建了一个wx/__init__.pdx
)。然后cimport
并根据需要调用函数:相关问题 更多 >
编程相关推荐