[Tutor] reassigning/replacing sys.stdout

Tony Cappellini cappy2112 at gmail.com
Tue Jan 30 09:54:10 CET 2007


got it -thanks

On 1/30/07, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
>
> Tony Cappellini wrote:
> >
> > I'm writing a python gui app that will call a python cmd line app. The
> > gui will take users input, pass it to the cmd line app, then display
> > the cmd app program output. The idea is to give users who aren't
> > comfortable with the cmd line a way to run the program, without having
> > to open a shell window (on Linux) or cmd prompt  (On Windows) and get
> > hung up on syntax or shell issues. It's a crutch.
> >
> > The author of the cmd line app suggested I temporarily replace
> > sys.stdout 'with a file descriptor class that can write directly to
> > the gui'.
> > The author is now out of communications for a few weeks,  so I can't
> > elaborate.
> >
> > However, I've opened a file which writes to the disk, and replaced
> > that sys.stdout with that file descriptor.
> > I can then close and open that file and have the output of his program.
> > Now I can just put this in a textbox on the gui and display it to the
> > user.
> >
> > Writing to a file on disk, then reading it back in seems a bit clunky
> > to me.
> > While I did somewhat do what the author had suggested (although I
> > didn't make a class out of the file descriptor), and it works, is
> > there a better way?
> print 'bob'
> is the same as
> sys.stdout.write('bob')
> sys.stdout.write('\n')
>
> In python, since this 'sys....' function call doesn't check what type of
> object it's calling,
> just that it has a method called 'write', you can write your own object
> and drop it in here.
>
> E.G.
>
> class GuiUpdater(object):
>     def write(astring):
>         #update your textbox here.
>
> sys.stdout = GuiUpdater
> print 'teststr'
>
> Hmm, actually that might not work since it's not an instance of a class.
> But that's the general idea/strategy.
>
> HTH,
> -Luke
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070130/adb94230/attachment.htm 


More information about the Tutor mailing list