[Tutor] Tkinter help

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 24 Aug 2001 14:04:55 -0700 (PDT)


On Fri, 24 Aug 2001, paul wrote:

> ok, can somebody give me an example of how to assign sys.stdout to a
> file and then print it into a text box?  i think i'm missing
> something.

This might help:  Here's an interpreter session that that traps everything
coming at sys.stdout into a StringIO object:

###
>>> import StringIO
>>> io = StringIO.StringIO()
>>> import sys
>>> sys.stdout = io
>>> print "Hello"
>>> print "This is a test."
>>> io.seek(0)
>>> sys.stderr.write(io.read())    ## You can replace this with the
                                   ## textbox insert() stuff.
Hello
This is a test.
###

When we use StringIO, we shouldn't forget to seek() the file object to the
beginning before we read() it: otherwise, we'll be holding the empty
string in our hands.