how can I use wxPython from Python embedded in a wxWidgets C++ application?

Hi group!

In a nutshell: I have a C++ application running a wxWidgets GUI. The
application has embedded Python. I'd like to use wxPython from Python
code executed by the embedded Python. How should I go on about it?

I'm in the process of writing the Python interface for hugin, the free
panorama stitcher. Hugin is a C++ application using wxWidgets for it's
GUI. I have wrapped hugin's functionality with SWIG and embedded
Python in the application, so I can call Python code, and with the
SWIG module imported I can pass hugin data to and fro. The intended
use of Python from hugin's main application is to provide Python
plugins to hugin. Therefore I'd like to let the Python code interact
with the user graphically, and wxPython seems the obvious candidate.

My first naive attempts to run Python code from hugin that
successfully uses wxPython standalone resulted in the application
crashing, just after the windows pop up. I suspect I need to transport
some state of the wxWidgets running in the C++ app to the Python code
wanting to use wxPython. I suppose this is known territory, but so far
I've failed to find the right information. If you could point me in
the right direction, I'd be very grateful indeed. I'm working on a
Kubuntu 10.10 system, but the application is cross-platform.

Kay

Take a look at the code in wxPython/samples/embedded/ for an example and some tips.

···

On 3/11/11 4:03 AM, kfj wrote:

Hi group!

In a nutshell: I have a C++ application running a wxWidgets GUI. The
application has embedded Python. I'd like to use wxPython from Python
code executed by the embedded Python. How should I go on about it?

I'm in the process of writing the Python interface for hugin, the free
panorama stitcher. Hugin is a C++ application using wxWidgets for it's
GUI. I have wrapped hugin's functionality with SWIG and embedded
Python in the application, so I can call Python code, and with the
SWIG module imported I can pass hugin data to and fro. The intended
use of Python from hugin's main application is to provide Python
plugins to hugin. Therefore I'd like to let the Python code interact
with the user graphically, and wxPython seems the obvious candidate.

My first naive attempts to run Python code from hugin that
successfully uses wxPython standalone resulted in the application
crashing, just after the windows pop up. I suspect I need to transport
some state of the wxWidgets running in the C++ app to the Python code
wanting to use wxPython. I suppose this is known territory, but so far
I've failed to find the right information. If you could point me in
the right direction, I'd be very grateful indeed. I'm working on a
Kubuntu 10.10 system, but the application is cross-platform.

--
Robin Dunn
Software Craftsman
http://wxPython.org

Thank you. This took a bit of searching, since it wasn't on my
machine. I eventually found it at

http://svn.wxwidgets.org/viewvc/wx/wxPython/trunk/samples/embedded/

In case someone else is looking for the same info.

Kay

···

On 11 Mrz., 18:53, Robin Dunn <ro...@alldunn.com> wrote:

Take a look at the code in wxPython/samples/embedded/ for an example and
some tips

On Ubuntu I think it is in the wx-examples package.

···

On 3/12/11 2:37 AM, kfj wrote:

On 11 Mrz., 18:53, Robin Dunn<ro...@alldunn.com> wrote:

Take a look at the code in wxPython/samples/embedded/ for an example and
some tips

Thank you. This took a bit of searching, since it wasn't on my
machine. I eventually found it at

http://svn.wxwidgets.org/viewvc/wx/wxPython/trunk/samples/embedded/

In case someone else is looking for the same info.

--
Robin Dunn
Software Craftsman
http://wxPython.org

It's not. that's why it took some searching. I finally found this:

http://bytes.com/topic/python/answers/694596-wxpython-c-app-using-wxwidgets

which pointed me to the repo and mentions that this example isn't in
the package. It might be a good addition to it, though. Anyway, since
I'm posting to this thread again, let me ask another question:

I've looked into the code in the example, and I have also experimented
some more with using wxPython from Python code in plugins that I call
from the Python embedded in the wxWidgets/C++ application. On my
system, which is Kubntu 10.10, I could create wxPython objects which
displayed and behaved normally (as far as I can tell) without any
explicit reference of the embedding application to the wxPython code.
So I wonder if this is merely luck, because all components should have
been built with the same compiler and linked dynamically - and maybe
also because I have a SWIG interface in the application already - or
whether this is to be expected everywhere?

My task at hand is not sharing wx objects between the wxWidgets side
of things and the wxPython side (as done in the 'embedding' example),
just merely using a few dialog boxes and such for the plugins so the
user can choose a file or click on a radio button. Hugin may require
tighter integration at some point, but for now, plugin-related GUI
activity is meant to be dealt with by the Python side exclusively. If
I could rely on easy availability of wxPython objects for the plugins
this would be a great help, and it would greatly facilitate the
creation of plugins.

Kay

···

On 12 Mrz., 20:20, Robin Dunn <ro...@alldunn.com> wrote:

On Ubuntu I think it is in the wx-examples package.

Yes, the warnings in the readme and the code comments are mainly for Windows systems. On other platforms as long as dynamic linking is used and the C++ application is using the same version of wx then things should go pretty smoothly. You should just need to worry about dealing properly with the GIL and there are API functions (as shown in the sample) available to help you with that.

···

On 3/13/11 3:28 AM, kfj wrote:

On 12 Mrz., 20:20, Robin Dunn<ro...@alldunn.com> wrote:

On Ubuntu I think it is in the wx-examples package.

It's not. that's why it took some searching. I finally found this:

http://bytes.com/topic/python/answers/694596-wxpython-c-app-using-wxwidgets

which pointed me to the repo and mentions that this example isn't in
the package. It might be a good addition to it, though. Anyway, since
I'm posting to this thread again, let me ask another question:

I've looked into the code in the example, and I have also experimented
some more with using wxPython from Python code in plugins that I call
from the Python embedded in the wxWidgets/C++ application. On my
system, which is Kubntu 10.10, I could create wxPython objects which
displayed and behaved normally (as far as I can tell) without any
explicit reference of the embedding application to the wxPython code.
So I wonder if this is merely luck, because all components should have
been built with the same compiler and linked dynamically - and maybe
also because I have a SWIG interface in the application already - or
whether this is to be expected everywhere?

--
Robin Dunn
Software Craftsman
http://wxPython.org