killua0083
killua0083

Reputation: 55

wxpython redirect the text to the textctrl in real-time

wxpython how to redirect the text to the textctrl in real-time

I know how to redirect the text , but the textctrl show the text until the process end, I want to show the text in real-time

import sys,time
import wx

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

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


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!"
        time.sleep(5)
        print "======End====="

# Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()

can you give me a full example code. I need it.

Upvotes: 0

Views: 3088

Answers (1)

Mike Driscoll
Mike Driscoll

Reputation: 33071

I'm not sure what you're looking for. Your code does redirect text in real-time as is. And it looks like you are already using my tutorial which shows how to redirect stdout. That article should have been enough to get you going in most situations.

You may also find this other article helpful though:

It shows how to redirect text from subprocesses to your text control.

Upvotes: 2

Related Questions