[Tutor] sending stdout to a text box

alan.gauld@bt.com alan.gauld@bt.com
Mon, 10 Sep 2001 13:59:56 +0100


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