[SOLVED] PyGtk : Redirecting the stdout on a gtk.TextView

Post here if you need help with creating a Graphical User Interface in Python.

[SOLVED] PyGtk : Redirecting the stdout on a gtk.TextView

Postby Floreal » Sun Nov 03, 2013 6:45 pm

Hi,

I try to redirect the result of the command "wget" to a gtk.TextView. I use pygtk2. I try several solutions. I found this solution :
Code: Select all
import gtk,pygtk
import subprocess
import gobject

class CommandTextView(gtk.TextView):
    def __init__(self,command):
        super(CommandTextView,self).__init__()

        self.command = command
    def run(self):
        proc = subprocess.Popen(self.command,shell=True,stdout=subprocess.PIPE)
        gobject.io_add_watch(proc.stdout,
                          gobject.IO_IN,
                          self.write_to_buffer)
    def write_to_buffer(self, fd, condition):
        if condition == gobject.IO_IN:
            char = fd.read(1)
            buf = self.get_buffer()
            buf.insert_at_cursor(char)
            return True
        else:
            return False

def test():
    win=gtk.Window()
    win.set_size_request(300,300)
    win.connect('delete-event',lambda w,e : gtk.main_quit())
    ctv=CommandTextView("wget http://download-origin.cdn.mozilla.net/pub/mozilla.org/firefox/releases/1.0.5/source/firefox-1.0.5-source.tar.bz2")
    win.add(ctv)
    win.show_all()
    ctv.run()
    gtk.main()

if __name__=='__main__': test()


Whereas it works perfectly with the command "ls", it doesn't work withe the command wget : with the command wget the results appears in my console but not in the gtk.TextView...

Can you help me please ?

Thanks in advance for your response and sorry for my bad english...

Floréal.
Last edited by Floreal on Sun Nov 03, 2013 10:38 pm, edited 1 time in total.
Floreal
 
Posts: 3
Joined: Sun Nov 03, 2013 6:41 pm

Re: PyGtk : Redirecting the stdout on a gtk.TextView

Postby hrs » Sun Nov 03, 2013 10:18 pm

I don't know gtk and I didn't read your code.

I think that ls writes to stdout and wget writes to stderr. wget has a flag to write to stdout instead. Specifics of wget may differ between platforms.
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: PyGtk : Redirecting the stdout on a gtk.TextView

Postby Floreal » Sun Nov 03, 2013 10:38 pm

hrs wrote:I don't know gtk and I didn't read your code.

I think that ls writes to stdout and wget writes to stderr. wget has a flag to write to stdout instead. Specifics of wget may differ between platforms.


Thanks a lot, you are wright !! If I replace stdout by stderr in my code it works perfectly ! :D
Floreal
 
Posts: 3
Joined: Sun Nov 03, 2013 6:41 pm

Re: [SOLVED] PyGtk : Redirecting the stdout on a gtk.TextVie

Postby Floreal » Sun Nov 03, 2013 11:41 pm

And I have also found how make wget writting to stdout insteed of stderr : you have to add "2>&1" at the end of the command. It works for me ! :)
Floreal
 
Posts: 3
Joined: Sun Nov 03, 2013 6:41 pm


Return to GUI

Who is online

Users browsing this forum: W3C [Linkcheck] and 2 guests