Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

calling python procedures from tcl using tclpython

1,669 views
Skip to first unread message

chand

unread,
Jul 7, 2005, 8:49:21 AM7/7/05
to
Hi..

I am writing a Gui in TCL and my backend code is python. I want to call
python procedure in tcl using tclpyhton. I want to know clearly how
this should be implemented.

let's say I have procedure test_function(arg1,arg2 ...) defined in
test.py.
I want to call this procedure in tcl. Let me know how this should be
achieved.
The doubt basically have is how the tcl code knows in which .py file
this procedure is defined.

currently I have wriiten this tcl code which is not working

package require tclpython
set interpreter [python::interp new]
$interpreter eval {def test_function(): arg1,arg2} ;
python::interp delete $interpreter

--Bestregards.,
--Chandra

Michael Schlenker

unread,
Jul 7, 2005, 9:07:56 AM7/7/05
to chand
What does not work? You never load your file test.py anywhere inside,
you just evaluate 'def test_function(): arg1,arg2' so what are you
expecting to happen?

Your call to '$interpreter eval' lets you call arbitrary python code,
but you have to provide the python code that loads your function
definitions etc. I don't know python, but it should be a trivial code.

Michael


chand

unread,
Jul 7, 2005, 9:39:44 AM7/7/05
to
That is exactly i want to know. i.e...,how to provide the python code
path in tcl script file, that loads required function.

--BestRegards.,
--chandra

> definitions etc.

chand

unread,
Jul 8, 2005, 12:11:48 AM7/8/05
to
Hi.,

can anyone help me how to provide the info about the python file
procedure in the tcl script which uses tclpython i.e., is there a way
to import that .py file procedure in the tcl script


--BestRegards
--Chandra

Jeff Hobbs

unread,
Jul 8, 2005, 7:56:46 PM7/8/05
to chand
chand wrote:
> can anyone help me how to provide the info about the python file
> procedure in the tcl script which uses tclpython i.e., is there a way
> to import that .py file procedure in the tcl script

>>>currently I have wriiten this tcl code which is not working


>>>
>>>package require tclpython
>>>set interpreter [python::interp new]
>>>$interpreter eval {def test_function(): arg1,arg2} ;
>>>python::interp delete $interpreter

You would call 'import' in the python interpreter, like so:
$interpreter eval { import testfile }
assuming it's on the module search path. Look in the python
docs about Modules to get all the info you need.

--
Jeff Hobbs, The Tcl Guy
http://www.ActiveState.com/, a division of Sophos

Jonathan Ellis

unread,
Jul 9, 2005, 1:34:05 PM7/9/05
to
Jeff Hobbs wrote:
> chand wrote:
> > can anyone help me how to provide the info about the python file
> > procedure in the tcl script which uses tclpython i.e., is there a way
> > to import that .py file procedure in the tcl script
>
> >>>currently I have wriiten this tcl code which is not working
> >>>
> >>>package require tclpython
> >>>set interpreter [python::interp new]
> >>>$interpreter eval {def test_function(): arg1,arg2} ;
> >>>python::interp delete $interpreter
>
> You would call 'import' in the python interpreter, like so:
> $interpreter eval { import testfile }
> assuming it's on the module search path. Look in the python
> docs about Modules to get all the info you need.

Actually, both your import and the original "def" problem need to use
exec instead of eval. Eval works with expressions; for statements you
need exec.

I blogged a brief example of tclpython over here:
http://spyced.blogspot.com/2005/06/tale-of-wiki-diff-implementation.html

-Jonathan

engelbert gruber

unread,
Jul 12, 2005, 3:42:13 AM7/12/05
to

why not this way ::

# tcl and python mixed coding
#
# condensed version of
#
# PyTix: Wrappers for the Tix Widget Set
#
# Mike Clarkson, Ioi Lam Nov. 30, 2000

tcl_code = """
proc tclHi {} {
puts "Hi from Tcl"
}

frame .frame -relief ridge -borderwidth 2
pack .frame -fill both -expand 1
label .frame.label -text "Hello, WOrld"
pack .frame.label -fill x -expand 1
button .frame.hello -text "Hello" \
-command [list tclHi]
pack .frame.hello -side top
button .frame.py -text "Hello too"
pack .frame.py -side bottom
button .frame.exit -text "Exit" \
-command [list destroy .]
pack .frame.exit -side bottom
"""

import Tkinter
root = Tkinter.Tk()
root.tk.eval(tcl_code)

def Hi():
print "Hi from Python"

root.tk.eval('.frame.py configure -command '+root.register(Hi))
root.mainloop()

0 new messages