Re: calling python procedures from tcl using tclpython

Discussion in 'Python' started by Michael Schlenker, Jul 7, 2005.

  1. chand wrote:
    > 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
    >

    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
     
    Michael Schlenker, Jul 7, 2005
    #1
    1. Advertisements

  2. chand

    chand Guest

    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.


    Michael Schlenker wrote:
    > chand wrote:
    > > 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
    > >

    > 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, Jul 7, 2005
    #2
    1. Advertisements

  3. chand

    chand Guest

    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

    Michael Schlenker wrote:
    > chand wrote:
    > > 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
    > >

    > 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, Jul 8, 2005
    #3
  4. Jeff Hobbs

    Jeff Hobbs Guest

    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
     
    Jeff Hobbs, Jul 9, 2005
    #4
  5. 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
     
    Jonathan Ellis, Jul 9, 2005
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean-Luc Fontaine

    ANNOUNCE: tclpython-3.1

    Jean-Luc Fontaine, Jun 25, 2003, in forum: Python
    Replies:
    0
    Views:
    792
    Jean-Luc Fontaine
    Jun 25, 2003
  2. Jean-Luc Fontaine

    ANNOUNCE: tclpython-3.1

    Jean-Luc Fontaine, Jul 8, 2004, in forum: Python
    Replies:
    2
    Views:
    466
    Roy Smith
    Jul 11, 2004
  3. comp.lang.python
    Replies:
    1
    Views:
    1,462
    Stephan Kuhagen
    Nov 21, 2006
  4. Alex. O. Koranteng
    Replies:
    8
    Views:
    650
    Alex. O. Koranteng
    Jun 16, 2009
  5. Mumia W.

    Inline::Tcl vs. Inline::Tcl

    Mumia W., Aug 23, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    310
    Mumia W.
    Aug 23, 2006
Loading...

Share This Page