Accessing wxPython objects from C?

Hi,

I'd like to access the underlying C++ objects (such as a wxWindow or a wxMenu) from wxPython objects within my python extension? Does anyone have any idea how I should do this - I took a look, but I'm afraid the SWIG stuff just ended up confusing me :slight_smile:

-Sterling

Sterling Hughes wrote:

Hi,

I'd like to access the underlying C++ objects (such as a wxWindow or a wxMenu) from wxPython objects within my python extension? Does anyone have any idea how I should do this - I took a look, but I'm afraid the SWIG stuff just ended up confusing me :slight_smile:

To just get access to the C++ objects you can do what I describe below. For making your own extensions that add additional wx classes to wxPython then see http://wiki.wxpython.org/index.cgi/C_2b_2bExtensions

To get access to the C++ objects wrapped by wxPython objects you need to include wxPython.h in your extension and call wxPyCoreAPI_IMPORT to "import" a set of functions from the core wxPython module into your extension module. (Actually it just sets a pointer to a structure of function pointers.) You can then use SWIG_GetPtrObj to get a pointer to the C++ wx object from a Python object. Use it like this:

  wxWindow* wPtr;
  if (SWIG_GetPtrObj(pyobj, (void **)&wPtr, "_wxWindow_p")) {
    // Some error happened, and a Python
    // exception has been set. React accordingly.
  }

Things like the above are changing a lot in 2.5 however so be sure to isolate your usage of it into some wrapper functions or something so it will be easier to migrate your code to 2.5.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!