mirroring text window to file
Alex Martelli
aleax at aleax.it
Sat Apr 12 05:58:00 EDT 2003
Marcel Achim wrote:
> I was wondering if there's an existing solution other than writing
> to both a file and an output text window (Tk text widget), as in :
>
> text_window.insert('end', output)
> log_file.write(output)
Depends to what "problem" you want an "existing solution" for. For
the purpose of having some string be appended to both a file and a
text window, you do need these two calls to happen _some_where, but
you can arrange for that to happen in several different ways, mostly
by exploiting the concept of "filelike objects" -- it all depends
on what, exactly, you're trying to accomplish. E.g.:
class LogAndText:
def __init__(self, thewindow, thefile):
self.thewindow = thewindow
self.thefile = thefile
def write(self, stuff):
self.thewindow.insert('end', stuff)
self.thefile.write(stuff)
now you can instantiate
logansho = LogAndText(text_window, log_file)
and just usr logansho as if it was a file open for output (e.g.
you can print>>logansho,whatever -- you can set logansho as
sys.stdout so that all normal print statements will use it -- &c).
Alex
More information about the Python-list
mailing list