Home > Software engineering >  How can you get what the function input in `trace_add` return (tkinter)
How can you get what the function input in `trace_add` return (tkinter)

Time:08-27

The callback function that is called when the variable you're listening to is changed / read / etc ... (according to mode) is called indeed, but I cannot find a way to get what the function returns. And I want it to modify a variable without having to start my listener with global my_variable, as using a global variable here is likely to not be recommended.

The documentation seems to indicate that callback can return stuff (the type-hinting doesn't specify -> None but -> Any. Cf. below:

enter image description here

CodePudding user response:

The type hint showing -> str means trace_add will return a str, which means, when you store what trace_add returns it will be a string. It does not refer to the return value from callback argument supplied to it:

abc = var.trace_add('write', func)
print(abc) # Ex: 2761646820160func

Which will store the name of the callback.

And you cannot use what func returns, because it is a callback. You might want to use global var_name inside func to use the variable name elsewhere rather than returning it.

  • Related