开发者

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开发者_Python百科 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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜