Pipe stdout && stderr to a TkLabel widget

rantingrick rantingrick at gmail.com
Mon Jan 26 16:03:13 EST 2009


OK, here is a simple example that will show you what i want to do.
Right now if you type print 'hello' in the entry and press <enter> you
will see braces in the label "{}". But if you type sys.stdou.write
("hello") you will see "{hello}" in the label. So i got the stdout
piping to the widget now, but it will not work with the print
statement. AND there are those braces around the string??? Any ideas?

from Tkinter import *
root = Tk()

class NewOut():
    def write(self, *arg):
        v.set(arg)

saveout = sys.stdout
newout = NewOut()
sys.stdout = newout

e = StringVar(root)
entry = Entry(root, textvariable=e, font=('Courier New', 12))
entry.pack(fill=X, expand=1, padx=5, pady=5)

v = StringVar(root)
Label(textvariable=v).pack(fill=X, expand=1)

def onReturn(event):
    try:
        exec(e.get())
        e.set('')
    except:
        print 'Command Invalid'

root.bind('<Return>', onReturn)
entry.focus_set()
root.mainloop()



More information about the Python-list mailing list