[Tutor] sending stdout to a text box
Bill Tolbert
bill_tolbert@bigfoot.com
Thu, 6 Sep 2001 17:20:56 -0400 (EDT)
Well, a week has gone by and I've got to give this thing another try.
I have a script which works well; it sends messages to the console when
run from a prompt. How can I catch this and route it to a text box in
tkinter?
I've read about the textvariable and StringVar() option but I can't get it
to work. Text boxes don't support textvariable and I can't get the stdout
to redirect to anything useful.
Can someone help?
Here's the code:
#Adapted from TKTextDemo.py by Martin Stevens, budgester@budgester.com
#Shamelessly lifted from Useless Python (www.lowerstandard.com/python)
from Tkinter import *
class App:
## Create GUI
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.t = StringVar()
# this line doesn't work
#self.text_box = Text(frame, textvariable=self.t)
self.text_box = Text(frame)
self.text_box.pack()
# this call to dobackup works but no redirect of stdout
self.dobackup = Button(frame, text="Do Backup",
command=self.backup)
self.dobackup.pack(side=TOP)
def backup(self):
import ICAREBackup
#self.t = sys.stdout
#sys.stdout = self.t
self.text_box.insert(END, sys.stdout)
ICAREBackup.ICAREBackup()
root = Tk()
app = App(root)
root.mainloop()
=-=-=-=-=-=-=-=-=
Bill Tolbert