Home > database >  What is `mode` argument of `trace_add` method in tkinter
What is `mode` argument of `trace_add` method in tkinter

Time:08-26

The documentation states: Mode: It is one of “array”, “read”, “write”, “unset”, or a list or tuple of such strings. but I'm not sure I understand what it means.

I'm using trace_add to call a function whenever my object tk.StringVar changes value and I'm using the mode write without really understanding why.

EDIT: what I find online is only about the deprecated method trace; nothing about the method trace_add.

CodePudding user response:

Using the value "write" means you want the callback to be called whenever the variable is written (ie: changed; when the set method is called).

Using the value "read" means you want the callback to be called whenever the variable is read (ie: the get method is called).

Using the value "unset" means you want the callback to be called whenever the variable is unset. In the case of tkinter, this happens when the variable is deleted.

The canonical tcl documentation for "array" says: "Invoke commandPrefix whenever the variable is accessed or modified via the array command". Tkinter doesn't directly give access to tcl's array command, so this one isn't very useful in tkinter.

  • Related