[Tutor] sending stdout to a text box

Kalle Svensson kalle@gnupung.net
Fri, 7 Sep 2001 13:12:27 +0200


[Bill Tolbert]
> 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?

Something like this might work:

> from Tkinter import *

class BogusFile:
    def __init__(self, text):
        self.text = text
    def write(self, s):
        self.text.insert(END, s)

> 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

          sys.stdout = BogusFile(self.text_box)

>         ICAREBackup.ICAREBackup()
> 
> root = Tk()
> 
> app = App(root)
> 
> root.mainloop()
> 

Incredibly untested, but might work.

Peace,
  Kalle
-- 
[  kalle@gnupung.net  ][ Thought control, brought to you by the WIPO! ]
[ http://gnupung.net/ ][ http://anti-dmca.org/ http://eurorights.org/ ]