I'm currently writing a Python program using Tkinter. In it, I want to trace the text within several entry boxes. Is there anyway I can pass parameters into the callback function that I call within the trace method? For example:
def cb(*args, var):
do something
some_object.trace("w", cb(var=some_var))
when I try this, I get a None Type error, which I'm assuming is because of how I'm attempting to pass in the argument.
You're calling the function immediately, and passing its return value as the callback argument, not passing the function as a callback.
Use lambda
to create a function that calls the function with an extra argument.
some_object.trace("w", lambda *args: cb(*args, var=some_var))
You can also use functools.partial
to create a function with some arguments pre-specified.
from functools import partial
some_object.trace("w", partial(cb, var=some_var))