wxpython: Redirect the stdout to a textctrl

kyosohma at gmail.com kyosohma at gmail.com
Tue Jun 19 23:07:10 CEST 2007


On Jun 19, 3:16 pm, Alejandro <alejandro.weinst... at gmail.com> wrote:
> 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




More information about the Python-list mailing list