0
votes

I have a tcl proc that takes 3 arguments. Two are strings and the other is another tcl proc. Basically:

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

Now, I need to call the handler proc from tkinter.

I have tried this:

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()

But the handler_sub function is not getting called (no error codes are returned, either). It seems that the two strings are passed to tcl without any issues, though.

Basically, I need to pass a python function to tcl code, somewhat like how I can set a python function to be called when a tk.Button is pressed. I'm sure that it is possible, but I can't figure out how to do it.

Any insights would be highly appreciated.

1

1 Answers

1
votes

You can call the register method of a widget to create a new tcl proc that will act as a proxy for the python function.

I think this is how it should be done:

def handler_sub(data):
    #Do some python magic

handler_proc = root.register(handler_sub)
randomwidget.tk.call(..., "-otherfunction", handler_proc)