Pipe stdout && stderr to a TkLabel widget

Discussion in 'Python' started by rantingrick, Jan 26, 2009.

  1. rantingrick

    rantingrick Guest

    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?
    rantingrick, Jan 26, 2009
    #1
    1. Advertising

  2. rantingrick

    rantingrick Guest

    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()
    rantingrick, Jan 26, 2009
    #2
    1. Advertising

  3. rantingrick

    rantingrick Guest

    Hello, anybody. Any help would be good help

    Thanks
    rantingrick, Feb 1, 2009
    #3
  4. rantingrick

    rantingrick Guest

    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
    rantingrick, Feb 1, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elad
    Replies:
    0
    Views:
    410
  2. Mathieu Prevot

    PIPE stderr

    Mathieu Prevot, May 24, 2008, in forum: Python
    Replies:
    0
    Views:
    281
    Mathieu Prevot
    May 24, 2008
  3. Matt Mencel
    Replies:
    1
    Views:
    172
    ara.t.howard
    Jul 7, 2008
  4. Ivan Novick

    pipe for stderr and stdout

    Ivan Novick, Mar 26, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    116
    Ted Zlatanov
    Mar 27, 2008
  5. Replies:
    0
    Views:
    332
Loading...

Share This Page