wxpython: Redirect the stdout to a textctrl

A

Alejandro

Hi:

I want to redirect stdout to a textctrl I have. From what I read in
the wxpython documentation, I can use the wxLogTextCtrl class to do
this. I am doing the following:

class MyGui(gui.MyFrame): #gui.MyFrame generated by wxGlade
def __init__(self, *args, **kwds):
gui.MyFrame.__init__(self, *args, **kwds)
# ... code removed ...
wx.Log_SetActiveTarget(wx.LogTextCtrl(self.text_ctrl_2))
print 'foo' #to test the redirection

if __name__ == "__main__":
app = wx.App(redirect=wx.LogTextCtrl)
wx.InitAllImageHandlers()
gui = MyGui(None, -1, "")
app.SetTopWindow(gui)
gui.Show()
app.MainLoop()

However, the output instead of going to my textctrl, goes to a new
window named "xwPython: stdout/stderr".

What am I missing?
 
K

kyosohma

Hi:

I want to redirect stdout to a textctrl I have. From what I read in
the wxpython documentation, I can use the wxLogTextCtrl class to do
this. I am doing the following:

class MyGui(gui.MyFrame): #gui.MyFrame generated by wxGlade
def __init__(self, *args, **kwds):
gui.MyFrame.__init__(self, *args, **kwds)
# ... code removed ...
wx.Log_SetActiveTarget(wx.LogTextCtrl(self.text_ctrl_2))
print 'foo' #to test the redirection

if __name__ == "__main__":
app = wx.App(redirect=wx.LogTextCtrl)
wx.InitAllImageHandlers()
gui = MyGui(None, -1, "")
app.SetTopWindow(gui)
gui.Show()
app.MainLoop()

However, the output instead of going to my textctrl, goes to a new
window named "xwPython: stdout/stderr".

What am I missing?

Give this a try:

<code>

class XPinst(wx.App):
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)

def OnInit(self):
self.frame = wx.Frame(None, -1, title='Redirect Test',
size=(620,450),
style=wx.STAY_ON_TOP|
wx.DEFAULT_FRAME_STYLE)

panel = wx.Panel(self.frame, -1)

self.log = wx.TextCtrl(panel, -1, size=(500,400),
style = wx.TE_MULTILINE|wx.TE_READONLY|
wx.HSCROLL)
redir=RedirectText(self.log)
sys.stdout=redir
print 'test'

self.frame.Show()
return True

class RedirectText:
def __init__(self,aWxTextCtrl):
self.out=aWxTextCtrl

def write(self,string):
self.out.WriteText(string)

</code>

Mike
 
A

Alejandro

Give this a try:

<code> [good piece of code]
</code>

It worked like a charm! Thanks.

I still don't understand why my initial aproach didn't work as
expected.

Regards,
Alejandro.
 
Joined
Aug 12, 2010
Messages
1
Reaction score
0
How do you get back

Sorry if this is an obvious question, but here goes. I'm using this redirection to see my data in a txtCtrl. When I close that panel, I want to go back to stdout.
How would I write a class to got back to stdout.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top