I am trying to redirect the stdout following the example in http://www.blog.pythonlibrary.org/2009/01/01/wxpython-redirecting-stdout-stderr/. My code has some loops, and I would like the function to redirect stdout each time the loop is repeated, as it happens when running the code with the command line. However, it seems that in this example the stdout is only redirected when the whole function is finished. How can I redirect the stdout in a "real time" mode? I attach my code.


Something like this:

app = wx.App(redirect=True)

and wx does the rest.

Cheers and Happy coding.

I like your solution. However, there are a couple of issues. First of all, it is not a "real time" redirection, as it only sends the stdout to the console when the function has finished. Then, is it possible to embed the console inside the main frame? I attach the fill code.


I've just tested, and to me they are both in real time.

import time
import wx

class Frame(wx.Frame):
    def __init__(self):

        super(Frame, self).__init__(None, -1, 'Output to widget in realtime')

        panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.HORIZONTAL)

        btn = wx.Button(panel, -1, 'Print RT')
        text = wx.TextCtrl(panel, -1, style=wx.TE_MULTILINE|wx.TE_READONLY)
        self.text = text

        sizer.Add(btn, 0, wx.ALL, 10)
        sizer.Add(text, 1, wx.EXPAND|wx.ALL, 10)


        self.Bind(wx.EVT_BUTTON, self.onbutton, btn)

    def onbutton(self, event):
        self.textoutput('Something like this?\n')
        for i in range(1, 11):
            self.textoutput('%s printed...\n' % i)
            print '%s printed...' % i

    def textoutput(self, text):
if __name__ == '__main__':
    app = wx.PySimpleApp(redirect=True)
    frame = Frame()

Cheers and Happy coding.

Thank you! I may be some problem with my computer, as it does not print the info in real time.


Make the window smaller so that You can see the output in separate window and main window same time, before clicking the button. You do start application from file manager, not from IDLE, don't you?

Yes, I start the app from file manager (Nautilus). When I click the go button the app frame turns grey.


Tried it in Ubuntu myself and the app behaves differently. Does not update realtime like in Windows XP

Any of them tonyjv?

Not even the internal print on the textbox??

Cheers and Happy coding.

Everything appears in the end, see captured screenshots after pushing the button.
I had time to save one screen shot and take other without prints coming up.