Pipe stdout && stderr to a TkLabel widget

R

rantingrick

I am wondering how i might pipe stdout && stderr to a Tkinter Label
widget. here are a few ways to set the text

#-- Create a label --#
label = Label(master, text='Default Text')
label.pack()

# -- two ways to change the text --#
label['text'] = 'New Text'
label.configure(text='New Text')

#-- or use a control variable --#
v = StringVar(master)
label.config(textvariable=v)

#-- Change useing variable --#
v.set('NewText')

So my question is -- I know hoe to set the text displayed in the label
wiget, but hoow do i capture stdout and send that to the widget?
 
R

rantingrick

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()
 
R

rantingrick

PS:

The braces are there because i used *arg in the fuction, so that is
not a problem now. All i want to do is overide the print statement to
sent all it's output to a Tkinter Label widget

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top