[Tutor] sending stdout to a text box

alan.gauld@bt.com alan.gauld@bt.com
Tue, 11 Sep 2001 13:05:27 +0100


> I'm trying to incorporate some utility scripts that use print 

Yes thats what my examnple did. All print statements executed 
by your program after the assignment of stdout will go to the 
text widget.


> 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?

Yes, by using the write() method it will put the output onto 
the screen as it happens. What I illustrated was how to avoid 
using a temporary file or String buffer(as Danny suggested in 
an earlier post)



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

Don't need this line if you set it up in __init__ of 
the Tkinter application as:
          sys.stdout = self


>         ICAREBackup.stdout = BogusFile(self.text_box)

Don't need this at all I think.


>         ICAREBackup.ICAREBackup()

The print statemewnts in ICAREBackup will call self.write()

> > 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
       def backup(self):
          ICAREBackup.ICAREBackup()  # likewise


Alan G.