changing focus of wxpython window embedded into C++

I'm looking to use wxpython to display information in running C++/SFML
programs.
I've just called this function, as a test...

void my_wxpython_eg(){
   Py_Initialize();
   PyRun_SimpleString(
      "from wxPython.wx import *\n"
      "class MyApp(wxApp):\n"
      " def OnInit(self):\n"
      " frame = wxFrame(NULL, -1, 'Hello from wxPython')\n"
      " frame.Show(true)\n"
      " self.SetTopWindow(frame)\n"
      " return true\n"
      "app = MyApp(0)\n"
      "app.MainLoop()\n"
   );
   Py_Finalize();
}

from a C++ SFML-based animation.
The little wxpython window pops up, and starves the sfml window of
focus,
until I close the wxpython window.

I will need this sort of blocking behaviour but do wonder how you
would...
   keep the wxpython window open and on top...
   switch focus back to the C++/SFML stepping simulation so...
   it can continue to step and also...
   update the wxpython window as it runs.

As an aside the wxpython window pops up to the far right of the screen
ie well away from the SFML window.
Is there some way I can specify it's x,y coordinates on the screen?

I hope I've made my question clear and look forward to your thoughts.

BTW how's porting to Python 3 going?

Best Regards

I now believe my problem is more to do with the fact that until
Py_Finalize() is called
C++ can not continue cos we're stuck in the Python interpreter.
If I want all GUI stuff in my C++ app to be wxpython based, which I
do,
this suggests having the C++ app as a Python extension
and using 2 way inter-process communication
ie
a) for C++ to instruct the wxpython GUI to update and for
b) the GUI/Python to tell the C++ process that it has updated and to
continue running.

I already use wxpython to specify paramaters and then actually
generate the C++ program in the first place.

It therefore seems sensible to use such an investment for the C++ part
of the app too.
I also use Python to serialise the C++ objects.

At the moment the C++ program generation and serialisation are done
using my own bindings.
They works but are ugly, very limited and inflexible.

Any light anyone can throw on this sort of thing would be very much
appreciated.

···

===============================================================

On Feb 25, 12:13 pm, bobl <mgbg25...@blueyonder.co.uk> wrote:

I'm looking to use wxpython to display information in running C++/SFML
programs.
I've just called this function, as a test...

void my_wxpython_eg(){
Py_Initialize();
PyRun_SimpleString(
"from wxPython.wx import *\n"
"class MyApp(wxApp):\n"
" def OnInit(self):\n"
" frame = wxFrame(NULL, -1, 'Hello from wxPython')\n"
" frame.Show(true)\n"
" self.SetTopWindow(frame)\n"
" return true\n"
"app = MyApp(0)\n"
"app.MainLoop()\n"
);
Py_Finalize();

}

from a C++ SFML-based animation.
The little wxpython window pops up, and starves the sfml window of
focus,
until I close the wxpython window.

I will need this sort of blocking behaviour but do wonder how you
would...
keep the wxpython window open and on top...
switch focus back to the C++/SFML stepping simulation so...
it can continue to step and also...
update the wxpython window as it runs.

As an aside the wxpython window pops up to the far right of the screen
ie well away from the SFML window.
Is there some way I can specify it's x,y coordinates on the screen?

I hope I've made my question clear and look forward to your thoughts.

BTW how's porting to Python 3 going?

Best Regards

I now believe my problem is more to do with the fact that until
Py_Finalize() is called
C++ can not continue cos we're stuck in the Python interpreter.

Yes. If you want more than one thing to be happening at the same time then you would most likely need to be using multi-threading or multi-processing.

If I want all GUI stuff in my C++ app to be wxpython based, which I
do,
this suggests having the C++ app as a Python extension
and using 2 way inter-process communication

Yes, making it an extension makes sense, although IPC is only really needed if the two parts are indeed separate processes in which case you wouldn't really need to make the new Python extension.

ie
a) for C++ to instruct the wxpython GUI to update and for
b) the GUI/Python to tell the C++ process that it has updated and to
continue running.

I already use wxpython to specify paramaters and then actually
generate the C++ program in the first place.

It therefore seems sensible to use such an investment for the C++ part
of the app too.
I also use Python to serialise the C++ objects.

At the moment the C++ program generation and serialisation are done
using my own bindings.
They works but are ugly, very limited and inflexible.

Any light anyone can throw on this sort of thing would be very much
appreciated.

There are a couple approaches that come to mind. First, you could pass a Python callable object (a function, method, etc.) to the code in the C++ extension module, and then when it is running and has some partial or completed results it can call that callable passing the data as parameters. Then it's up to the Python callable what to do with that data, and as soon as it returns then the C++ part is able to continue. In order to run both this code and the GUI at the same time you'll probably want to have the extension code running in a worker thread, so the Python callable can be as simple as tossing the data over to the gui thread and then returning immediately.

The second approach is similar, but instead of a callback approach you would design the extension to do its work in smaller chunks, and then call each part of the algorithm from Python, dealing with the partial results along the way by updating the GUI thread, etc.

···

On 2/26/10 6:23 AM, bobl wrote:

--
Robin Dunn
Software Craftsman

Robin
Thanks very much for your advice and alternative solutions.
I'll certainly investigate both of them.

I've just been looking at Python's popen2 replacement
(subprocess/Popen pipes to stdin/out) as another possible solution
and do take your point that such two-way comms is unnecessary using
Python extensions.

On the subject of connecting Python/C++ I was looking at boost.python
and came across a pdf of yours re managing 1.6mloc of wxpython using
SWIG.

From what I've read boost supports C++ better but I can't see how
anything can be much better than managing 1.6mloc. Do you have any
thoughts re the two??

Best Regards
Dean

It's been a long time since I looked at Boost Python but it is essentially a template-heavy C++ class hierarchy, and you need to write a lot of code that implements the Python wrappers. OTOH, with tools like SWIG you just need to give the tool the function declaration and it generates the wrapper. There are lots of pros and cons for each approach.

···

On 2/26/10 12:17 PM, bobl wrote:

Robin
Thanks very much for your advice and alternative solutions.
I'll certainly investigate both of them.

I've just been looking at Python's popen2 replacement
(subprocess/Popen pipes to stdin/out) as another possible solution
and do take your point that such two-way comms is unnecessary using
Python extensions.

On the subject of connecting Python/C++ I was looking at boost.python
and came across a pdf of yours re managing 1.6mloc of wxpython using
SWIG.

From what I've read boost supports C++ better but I can't see how
anything can be much better than managing 1.6mloc. Do you have any
thoughts re the two??

--
Robin Dunn
Software Craftsman

My code is only 50kloc (50/50 re python & C++) and I doubt that it's
simplicity warrant's a heavily templated approach. I also like the
sound of SWIG's minimal coding so it makes sense to start with SWIG.

Thank you for your thoughts.