New programmers start using Python and wxPython each week. So it follows that every few months, I see people asking how to redirect stdout to a wx.TextCtrl on comp.lang.python or the wxPython mailing list. Since this is such a common question, I thought I would write an article about it. Regular readers will know that I have mentioned this concept before in a previous post.

Updated 2015-10-06

Originally I thought we’d need to create a class that can duck-type the writing API of a wx.TextCtrl. Notice that I use the so-called “new style” class that subclasses object (see code below).

class RedirectText(object): def __init__(self,aWxTextCtrl): self.out=aWxTextCtrl def write(self,string): self.out.WriteText(string)

Note that there’s only one method in this class (besides the initialization method, of course). It allows us to write the text from stdout or stderr to the text control. It should be noted that the write method is not thread-safe. If you want to redirect text from threads, then change the write statement like this:

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

Now let’s dig into the wxPython code we’ll need:

import sys import wx class MyForm(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY, "wxPython Redirect Tutorial") # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) log = wx.TextCtrl(panel, wx.ID_ANY, size=(300,100), style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL) btn = wx.Button(panel, wx.ID_ANY, 'Push me!') self.Bind(wx.EVT_BUTTON, self.onButton, btn) # Add widgets to a sizer sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(log, 1, wx.ALL|wx.EXPAND, 5) sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5) panel.SetSizer(sizer) # redirect text here redir=RedirectText(log) sys.stdout=redir def onButton(self, event): print "You pressed the button!" # Run the program if __name__ == "__main__": app = wx.App(False) frame = MyForm().Show() app.MainLoop()

In the code above, I create a read-only multi-line text control and a button whose sole purpose is to print some text to stdout. I add them to a BoxSizer to keep the widgets from stacking on top of each other and to better handle resizing of the frame. Next I instantiate the RedirectText class by passing it an instance of my text control. Finally, I set stdout to the RediectText instance, redir (i.e. sys.stdout=redir).

If you want to redirect stderr as well, then just add the following right after “sys.stdout=redir”: sys.stderr=redir

Improvements could be made to this, namely color coding (or pre-pending) which messages are from stdout and which are from stderr, but I’ll leave that as an exercise for the reader.

Recently it was pointed out to me that I shouldn’t need to jump through all these hoops. Instead, you can just redirect stdout directly to the TextCtrl widget because it has its very own write method. So here’s an example:

import sys import wx class MyForm(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, title="wxPython Redirect Tutorial") # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL log = wx.TextCtrl(panel, wx.ID_ANY, size=(300,100), style=style) btn = wx.Button(panel, wx.ID_ANY, 'Push me!') self.Bind(wx.EVT_BUTTON, self.onButton, btn) # Add widgets to a sizer sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(log, 1, wx.ALL|wx.EXPAND, 5) sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5) panel.SetSizer(sizer) # redirect text here sys.stdout=log def onButton(self, event): print "You pressed the button!" # Run the program if __name__ == "__main__": app = wx.App(False) frame = MyForm().Show() app.MainLoop()

You will note that the code above no longer references the RedirectText class because we don’t need it. I am pretty sure that if you want to use threads, doing it this way will not be thread-safe. You’ll need to override the TextCtrl’s write method in a similar manner as was previously mentioned to make it safe. Special thanks goes to carandraug for pointing this out to me.

Related Reading

Download the Source