redirect sys.stdout to a wx.TextCtrl widget

676 Views Asked by At

I'm trying to redirect the sys.stdout stream to a wx.TextCtrl widget. I've managed to get the stream to redirect but apparently some messages get writted twice. Hopefully somebody can explain this to me.

I'm using python 2.7.5 with wx 2.8.12.1, running from IPython 1.1.0.

import sys
import wx


class Example(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, 1, title='Example', size=(500, 500))

        console = Console(self)
        sys.stdout = console

    # Restore default stdout stream
    def __del__(self):
        Destroy()

    def Destroy(self):
        sys.stdout = sys.__stdout__


class Console(wx.TextCtrl):
    def __init__(self, parent):
        self.txtctrl = wx.TextCtrl(parent, style=wx.TE_MULTILINE | wx.TE_AUTO_SCROLL | wx.TE_RICH2)

    def write(self, message):        
        self.txtctrl.AppendText('>>> ')
        self.txtctrl.AppendText(message)


def main():
    app = wx.App()

    frame = Example()
    frame.Center()
    frame.Show()

    app.MainLoop()

if __name__ == '__main__':
    main()
0

There are 0 best solutions below