471,342 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,342 software developers and data experts.

wxpython: Redirect the stdout to a textctrl

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_ct rl_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?

Jun 19 '07 #1
3 5943
On Jun 19, 3:16 pm, Alejandro <alejandro.weinst...@gmail.comwrote:
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_ct rl_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

Jun 19 '07 #2
On Jun 19, 5:07 pm, kyoso...@gmail.com wrote:
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.
Jun 20 '07 #3
On Jun 20, 4:03 pm, Alejandro <alejandro.weinst...@gmail.comwrote:
On Jun 19, 5:07 pm, kyoso...@gmail.com wrote: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.
Alejandro,

Yeah, I'm not sure what the deal is with your code. But I've never
used the LogTextCtrl widget. You might ask why it doesn't work
directly by posting to the wxPython user's group here:
http://www.wxpython.org/maillist.php

Mike

Jun 20 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Logan | last post: by
1 post views Thread by sillyemperor | last post: by
1 post views Thread by justme | last post: by
1 post views Thread by [david] | last post: by
4 posts views Thread by Jimmy | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.