[Tutor] sending stdout to a text box

Bill Tolbert bill_tolbert@bigfoot.com
Mon, 10 Sep 2001 09:30:46 -0400 (EDT)


Thanks for the reply Alan. Kalle posted a good example too (I'm the one
who asked originally). I'm close, but not quite where I want to be.

I'm trying to incorporate some utility scripts that use print statements
to tell the user what's going on (success, error, busy, etc). I want my
gui to consolidate these and just redirect the existing print statements
to the text widget on the gui.

Output from ICAREBackup goes to BogusFile and on to the text box, but not
until ICAREBackup finishes. I was looking for a way to provide
immediate feedback. Some of these utilities could run for a very long
time, leaving the user confused and ready to reboot without
feedback. Output to the shell window is immediate; it doesn't wait
until the script finishes. Can I duplicate that immediate feedback?

    def backup(self):
        import ICAREBackup
        sys.stdout = BogusFile(self.text_box)
        ICAREBackup.stdout = BogusFile(self.text_box)
        ICAREBackup.ICAREBackup()

Thanks guys,

Bill

On Mon, 10 Sep 2001 alan.gauld@bt.com wrote:

> > > run from a prompt. How can I catch this and route it to a 
> > text box in tkinter?
> 
> I posted a short example of this but it wasn't wondersfully commented.
> 
> 
> Essentially you make sys.stdout point to an object which has 
> a write() method. (This could be your Tkinter application for 
> example)
> 
> Now when you call print the output will go to the new object.
> 
> Thus
> 
> class MyApp(Frame):
>    def __init__(self):
>      # do stuff here
>      self.display = Text(....)  # create our text widget
>      sys.stdout = self          # Set stdout here
> 
>    def write(self, s):   # provide the necessary write method
>      self.display.insert(END.s)  # append the string s to the widget
> 
>    # other methods as usual...
>    def foo(self):
>       print "This is foo"	  # will appear in text widget
> 
> Hope thats a little clearer.
> 
> Alan G.
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



=-=-=-=-=-=-=-=-=
Bill Tolbert