I put this into a new thread, because the previous location is a bit
obscure. The discussion is about showing a python plugin's (or any
other) console output in a wxWidgets window, which I think I have
worked out but I need a bit of wxWidgets help.
On 18 Jun., 04:55, Yuval Levy <
goo...@levy.ch> wrote:
> On June 17, 2011 06:06:20 AM kfj wrote:
>
> > On 15 Jun., 14:25, Yuval Levy <
goo...@levy.ch> wrote:
> > > What I still would like to see (don't know if it is possible/feasible in
> > > short time as the release is more important) is a pop up window like the
> > > one of hugin-stitch_project that displays the console output in the GUI.
>
> > I looked into the matter a bit yesterday.
>
> Thank you!
>
> > The problem was just that python's print statements seem to go
> > directly to the underlying stdio handles (stdin, stdout and stderr)
> > instead of C++'s cin, cout, cerr - so redirecting the output worked
> > only for any operator<< calls within C++. Maybe python can be coerced
> > to do things differently and use cin etc instead.
I've found an elegant, simple solution the the problem, but I need a
bit of help on the wxWidgets side to make it work nicely. This is what
I do - with a bit of background to show how and why it works:
1) wxWidgets has a class wxStreamToTextRedirector. This class can
redirect C++ stream output to a wxText widget. This works just fine,
but needs more effort for python's output, as python does not output
to C++ cout or cerr, but to it's notion of standard output, a python
object called stdout which can be accessed via the sys module.
2) python's sys.stdout is not some god-given immutable thing, but it
can be assigned any object that has a write() method. All python
output to stdout - so, that's print statements etc. - goes to that
object's write method. Easy to redirect python stdout.
3) hsi has already wrapped std::ios, so cout and cerr are available to
python
4) to put it all together. In python, I create a class echo (I do this
in hsi.py, so it's done automatically for every plugin call):
import hsi
import sys
class echo :
def write ( self , s ) :
hsi.cout.write ( s , len ( s ) )
hsi.cout.flush()
Then I assign an instance of class echo to python's sys.stdout:
sys.stdout = echo()
now all python output (and, which is an added bonus, all output from
any subprocesses python launches) goes to hugin's C++ cout and can be
redirected with a wxStreamToTextRedirector.
5) integrate the wxStreamToTextRedirector in hugin. This is where I
need help. I have managed to code enough to demonstrate that the
rerouting actually works, but my knowledge of wxWidgets is
insufficient. To demonstrate the method, I'm creating the redirector
in wxPanoCommand.cpp, in processPanorama():
bool PythonScriptPanoCmd::processPanorama(Panorama& pano)
{
wxTextCtrl *text = new wxTextCtrl(MainFrame::Get(),
-1,
wxString(),
wxPoint(0,0),
wxSize(600,600),
wxTE_MULTILINE | wxTE_READONLY
);
wxStreamToTextRedirector redirect(text);
// the redirect is now in effect.
// ... stuff happens, all cout output is redirected to 'text'
return true;
}
Obviously tis is amateurishly done. What happens is that the text
window only shows after the plugin has terminated - it does have all
the text inside that it should have, though. I suspect that the
wxTextCtrl has to be put inside it's own Window, which has to be shown
explicitly before the plugin starts and closed once the plugin is
done. Apart from that I think the problem is solved. Help with the
last step would be appreciated.
Kay