简体   繁体   English

使用函数指针作为Python的参数调用Tcl过程

[英]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? 是否可以从Python调用具有函数指针(或回调函数)的Tcl过程? I am using Tkinter to call Tcl procedures from Python. 我正在使用Tkinter从Python调用Tcl过程。

Python Snippet : Python片段:

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

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

Tcl Snippet : Tcl片段:

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

Note I cannot modify Tcl code as its an external library to my application. 注意我不能将Tcl代码修改为我的应用程序的外部库。

//Hemanth // Hemanth

Yes, and your pseudocode is pretty close. 是的,你的伪代码非常接近。 You have to register your python code with the Tcl interpreter. 您必须使用Tcl解释器注册您的python代码。 This will create a tcl command that will call your python code. 这将创建一个调用python代码的tcl命令。 You then reference this new tcl command whenever you pass it to a Tcl procedure that expects a procedure name. 然后,每当将其传递给需要过程名称的Tcl过程时,都会引用此新tcl命令。 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 http://epydoc.sourceforge.net/stdlib/Tkinter.Misc-class.html#register

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM