This shows how to build a wxPython extension using Visual Studio in a stand-alone project. These instructions describe experiences with wxPython 2.6.1.0 and the patched swig 1.3.24. As things get easier with newer versions of wxPython and swig, please leave the existing parts of the page, and add the newer information.
For the recommended method of extending wxPython with distutils, placing your source in the wxPython source tree please see: the C++Extenions page, and if you're curious how to use Scons to build a wxPython extension see the SconsExtensions page.
Why Bother?
- As a programmer who has done Assembly, C++ for a couple of decades and Java for a few years, I've grown some opinions and principles that all have to do with writing code that "stays." Anything that doesn't have any purpose a year or two from now is a waste of my development time now. Writing applications in a hybrid of wxPython and C++ is the closest I have found to a development method that gives me full control to write the performance sensitive, or bit-twidling parts of the application in C++, and all the higher level parts in a brilliant, concise high level language.
Getting Robin's wxPython build files
- SWIG needs the .i files that were used to wrap wxWidgets in order to know that you are working with the same classes that already exist in wxPython. These .i files and the binary lib files that correspond to the version of wxPython that you have installed are available on the wxPython download page. These unpack into a directory called wxPython-2.6.1.0. (This might be obsolete with the release of 2.6.2.x already.) The .i files are in the wxPython-2.6.1.0\include\wx\wxPython\i_files directory. The core.i file is the one that you usually include when you need most of wxWidgets, but you might need to also include other .i files from your SWIG interface file ocassionally.
Get SWIG, the Simple Wrapper Interface Generator
- Building swig on windows requires either Cygwin or MSYS to do a UNIX-like ./configure, make. I've done both, and I found the MSYS build easier, and the final swig.exe doesn't depend on cygwin1.dll which it does if you use cygwin. So first, install a MSYS environment, then unpack SWIG, creating a directory SWIG-1.3.24. Next download the 1.3.24 patch to the same directory so you should have the file "swig-1.3.24.patch" in the SWIG-1.3.24 directory. (I grabbed the patch file from the wxWidgets CVS HEAD, but google should find a download for you.) Use the command line "patch" utility to apply the patch "patch -i swig-1.3.24.patch". The SWIG patch was created against a CVS directory tree, so patch may not be able to find the files that the patch needs to change. I ended up typing in the correct path and file name for each one of the sections (it didn't take long.) Now, you can use configure and make to build the executable. In the end you have swig.exe along with its lib directory.
Writing a C++ wxWidgets class
- I'll have a good example someday, the code I wrote belongs to my employer. The Foobutton from the C++Extensions page is a good example to use for now.
Create the SWIG interface file
/*** tileCache.i *************************************************** */ %{ #include "wx/wxPython/wxPython.h" #include "imaging/big/tileCache.h" %} %import core.i %pythoncode { import wx } %pythoncode { __docfilter__ = wx._core.__DocFilter(globals()) } class TileCache: public TileFilter { public: TileCache(); // Notice: No wrapper for the destructor void SetCacheSize(int size); virtual wxImage GetTile(unsigned int tilex, unsigned int tiley); }; /* end of tileCache.i */
Visual Studio build environment
I'm going to defer to http://www.geocities.com/foetsch/python/extending_python.htm for the most part. In addition to setting up a custom build step, make sure you have C++ Preprocessor defines: SWIG_TYPE_TABLE=_wxPython_table;NDEBUG;WIN32;WIN32;_WINDOWS;_USRDLL;WXUSINGDLL C++ Code Generaton, Runtime library: Multi-threaded DLL
References
The Visual Studio DLL project came from this page: http://www.geocities.com/foetsch/python/extending_python.htm Eventually you'll need to know something about how SWIG works: http://www.swig.org/Doc1.1/HTML/Python.html