Tcl procedure call with function pointers as an argument from Python

Is it possible to call Tcl procedures containing function pointers (or callback functions) from Python? I use Tkinter to call Tcl procedures from Python.

Python snippet:

proc callbackFunc(): print "I am in callbackFunc" cb = callbackFunc Tkinter.Tk.call('tclproc::RetrieveInfo', cb) 

Fragment of Tcl:

 proc tclproc::RetrieveInfo() { callback } { eval $callback } 

Note. I cannot change the Tcl code as my external library for my application.

// Hemant

+6
python tcl tkinter
source share
1 answer

Yes, and your pseudo code is pretty close. You must register your Python code using the Tcl interpreter. This will create a tcl command that will call your python code. You then refer to this new tcl command whenever you pass it to the Tcl procedure, which expects the name of the procedure. This happens something like this:

 import Tkinter root=Tkinter.Tk() # create a python callback function def callbackFunc(): print "I am in callbackFunc" # register the callback as a Tcl command. What gets returned # must be used when calling the function from Tcl cb = root.register(callbackFunc) # call a tcl command ('eval', for demonstration purposes) # that calls our python callback: root.call('eval',cb) 

Below is a small piece of documentation:

http://epydoc.sourceforge.net/stdlib/Tkinter.Misc-class.html#register

+7
source share

All Articles