[Tutor] I'm tring to translate a Tcl/Tk program into Python.

Jeff Shannon jeff@ccvcorp.com
Thu, 13 Jun 2002 10:52:05 -0700


SA wrote:

> Hi Everyone-
>
>     I am trying to translate a tcl program that use tk into python. I seem
> to be going along fine until I get to the process that wants to "catch" the
> results onto one text screen. Here is what I am looking at:
>
> proc python {} {
>     catch {exec /sw/bin/python -c [.t1 get 0.0 end]} output
>     .t2 delete 0.0 end
>     .t2 insert end $output
> }

Where you're doing catch {exec ...} in tcl, for Python you'll want to look into
using os.popen('command ...'), which will start a new process execute 'command',
and returns a file-object that represents the stdout of the new process.  You
can then get that output by calling out.readlines(), or whatever.  (Note that
reading more from this file than is currently available, can cause your program
to block.)

Of course, if the command that you're executing is another Python script, you
may be better off just importing it instead of spawning a new shell and
interpreter for it...  :)

Jeff Shannon
Technician/Programmer
Credit International