Translating Tcl/Tk code into Python/Tkinter help please...

Thomas Sicheritz-Ponten thomas at cbs.dtu.dk
Fri Jun 14 04:59:21 EDT 2002


SA <sarmstrong13 at mac.com> writes:

> 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
> }
> 
> Basically, this process catches the output from running the code in the text
> widget .t1 through python -c and places it into the variable ouput.
> The text widget .t2 is then cleared and the contents of the variable $output
> are displayed in the text widget .t2.
> 
> I can translate all of the widgets into python/tkinter code, but I'm not
> sure about how to translate this process. Is there a function similar to
> "catch" in Python?

"try" and "exec" is used instead of "catch"
but in this case you could use commands.getoutput

Untested code:

import commands
from Tkinter import *


def execute_python_code1():
    global t1, t2

    txt = t1.get(0.0, END)
    com = "/sw/bin/python -c %s" txt
    
    try: 
        output = commands.getoutput(com)
    except:
        output = 'Error running command: %s' % com

    t2.delete(0.0, END)
    t2.insert(END,output)


    
# getoutput returns errors as catch does, so there is no need to use try/except
# IMHO, you should strip newlines from the command returned from the text widget

def execute_python_code2():
    t2.delete(0.0, END)
    output = commands.getoutput(com = "/sw/bin/python -c %s" % t1.get(0.0, END).strip())
    t2.insert(END,output)
        
        

-- 
Sicheritz-Ponten Thomas, Ph.D, thomas at biopython.org      (
Center for Biological Sequence Analysis                   \
BioCentrum-DTU, Technical University of Denmark            )
CBS: +45 45 252485      Building 208, DK-2800 Lyngby  ##----->
Fax: +45 45 931585      http://www.cbs.dtu.dk/thomas       )
                                                          /
     ... damn arrow eating trees ...                     (



More information about the Python-list mailing list