将 python 函数传递给 tcl
Pass python function to tcl
我有一个带 3 个参数的 tcl proc。两个是字符串,另一个是另一个 tcl proc。
基本上:
proc handler_sub {data}{
[<Do more stuff with data>]
}
proc handler {stringone stringtwo otherfunction} {
[<Do stuff with stringone and stringtwo, send output to otherfunction>]
}
.randomwidget handler -stringone $randomstrring -stringtwo $anotherrandomstrring -otherfunction handler_sub
现在,我需要从 tkinter 调用 handler
过程。
我试过这个:
import tkinter
root = tkinter.Tk()
randomwidget = SomeCustomWidget(root)
def handler_sub(data):
#Do some python magic
randomwidget.tk.call(randomwidget._w, "handler", "-stringone", randomstrring, "-stringtwo", anotherrandomstrring, "-otherfunction", handler_sub)
randomwidget.pack()
root.mainloop()
但是 handler_sub
函数没有被调用(也没有返回错误代码)。不过,这两个字符串似乎毫无问题地传递给了 tcl。
基本上,我需要将一个 python 函数传递给 tcl 代码,有点像我如何设置 python 函数以在按下 tk.Button
时调用。我确信这是可能的,但我不知道该怎么做。
如有任何见解,我们将不胜感激。
您可以调用小部件的 register
方法来创建一个新的 tcl proc,它将充当 python 函数的代理。
我认为应该这样做:
def handler_sub(data):
#Do some python magic
handler_proc = root.register(handler_sub)
randomwidget.tk.call(..., "-otherfunction", handler_proc)
我有一个带 3 个参数的 tcl proc。两个是字符串,另一个是另一个 tcl proc。 基本上:
proc handler_sub {data}{
[<Do more stuff with data>]
}
proc handler {stringone stringtwo otherfunction} {
[<Do stuff with stringone and stringtwo, send output to otherfunction>]
}
.randomwidget handler -stringone $randomstrring -stringtwo $anotherrandomstrring -otherfunction handler_sub
现在,我需要从 tkinter 调用 handler
过程。
我试过这个:
import tkinter
root = tkinter.Tk()
randomwidget = SomeCustomWidget(root)
def handler_sub(data):
#Do some python magic
randomwidget.tk.call(randomwidget._w, "handler", "-stringone", randomstrring, "-stringtwo", anotherrandomstrring, "-otherfunction", handler_sub)
randomwidget.pack()
root.mainloop()
但是 handler_sub
函数没有被调用(也没有返回错误代码)。不过,这两个字符串似乎毫无问题地传递给了 tcl。
基本上,我需要将一个 python 函数传递给 tcl 代码,有点像我如何设置 python 函数以在按下 tk.Button
时调用。我确信这是可能的,但我不知道该怎么做。
如有任何见解,我们将不胜感激。
您可以调用小部件的 register
方法来创建一个新的 tcl proc,它将充当 python 函数的代理。
我认为应该这样做:
def handler_sub(data):
#Do some python magic
handler_proc = root.register(handler_sub)
randomwidget.tk.call(..., "-otherfunction", handler_proc)