[Tutor] providing a Python command line within a Tkinter appl

Garry Willgoose garry.willgoose at newcastle.edu.au
Tue Jan 1 06:58:07 CET 2008

I'm writing a platform independent environmental modelling framework  
where I provide a GUI (in Tkinter) to a range of env modeling tools  
in a number of modules written in fortran, C and Python. It all works  
well but for generality I'd like to provide the user with a command  
line where he can do analyses using Python that are not provided by  
the framework's tools (e.g. some one off analysis either using Python  
directly or perhaps linking to another program like R using RPy,  
etc). However, once I kick off Tkinter's event loop I need  to  
provide a window where Python commands are entered and interpreted  
(Think the command line for Matlab or R but with a serious GUI and  
language ... lets not go down the route of why I didn't use these  
packages in the first place). Plan A was that I'd somehow like to use  
the python interpreter for this but I can't find any obvious way to  
do this. Plan B would seem to be to simply provide a text entry  
window and to interpret each line entered by the user using eval and  
providing sensible error messages. So I'd have a loop executed for  
each line entered that looks like

text =my_get_pythoncommand()   	# text is the line of text entered in  
the window by the user
   my_print_pythoncommand_result(result)	# echoing the result of the  
command back to the user
except error1:
   some error message
except error2:
   some other error message
except error3:
   ... etc ...
   some generic error message for unrecognised errors

The question is does this make sense or is there an easier way,  
particularly one where I'd be able to get the same error messages  
provided by the command line python interpreter?

I guess the other question I have is if this the way to go, are there  
any gotchas re the Python code I can execute in this way. Remember I  
am trying to provide a means of doing analyses  that are not provided  
by the framework so it would nice to just say to the user "Enter  
anything that is valid Python ... just like in the Python interpreter  
or IDLE".

Prof Garry Willgoose,
Australian Professorial Fellow in Environmental Engineering,
Director, Centre for Climate Impact Management (C2IM),
School of Engineering, The University of Newcastle,
Callaghan, 2308

Centre webpage: www.c2im.org.au

Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574  
(Fri PM-Mon)
FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal  
and Telluric)
Env. Engg. Secretary: (International) +61 2 4921 6042

email:  garry.willgoose at newcastle.edu.au;  
g.willgoose at telluricresearch.com
email-for-life: garry.willgoose at alum.mit.edu
personal webpage: www.telluricresearch.com/garry
"Do not go where the path may lead, go instead where there is no path  
and leave a trail"
                           Ralph Waldo Emerson

More information about the Tutor mailing list