Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Tcl procedures with Function pointers as argument from Python

Is it possible to call Tcl procedures that have function pointers (or callback functions) from Python? I am using Tkinter to call Tcl procedures from Python.

Python Snippet :

proc callbackFunc():
    print "I am in callbackFunc"

cb = callbackFunc
Tkinter.Tk.call('tclproc::RetrieveInfo', cb)

Tcl Snippet :

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

Note I cannot modify Tcl code as its an external library to my application.

//Hemanth

like image 444
Hemanth Avatar asked Sep 21 '10 19:09

Hemanth


1 Answers

Yes, and your pseudocode is pretty close. You have to register your python code with the Tcl interpreter. This will create a tcl command that will call your python code. You then reference this new tcl command whenever you pass it to a Tcl procedure that expects a procedure name. It goes 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)

A tiny bit of documentation is here:

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

like image 132
Bryan Oakley Avatar answered Oct 11 '22 13:10

Bryan Oakley