Tracing Tkinter variables in Python - GeeksforGeeks

12 Dec, 2021
There is no inbuilt way to track variables in Python. But tkinter supports creating variable wrappers that can be used to do so by attaching an ‘observer’ callback to the variable. The tkinter.Variable class has constructors like BooleanVar, DoubleVar, IntVarand StringVar for boolean, double-precision floating-point values, integer and strings respectively, all of which can be registered to an observer that gets triggered every time the value of the variable is accessed. The observer remains active until it is explicitly deleted. It is also important to note that the callback function associated with the observer takes three arguments by default i.e. Name of the Tkinter variable, index of the tkinter variable in case it’s an array otherwise an empty string and the mode of access.In Python 3.5 and 2.7 above older methods such as trace_variable(), trace(), trace_vdelete () and trace_vinfo() are replaced by the below mentioned methods. 
trace_add (): The trace_add () method has replaced trace_variable() method. The trace_add() method is used to add an observer to a variable and returns the name of the callback function whenever the value is accessed. 
trace_add (): The trace_add () method has replaced trace_variable() method. The trace_add() method is used to add an observer to a variable and returns the name of the callback function whenever the value is accessed. 
Syntax : trace_add(self, mode, callback_name) Parameters: Mode: It is one of “array”, “read”, “write”, “unset”, or a list or tuple of such strings. callback_name: It is the name of the callback function to be registered on the tkinter variable.  
 trace_remove():The trace_remove() method replaces trace_vdelete() method and it is used to unregister an observer. It returns the name of the callback used while registering the observer through trace_add() method initially.  
 
trace_remove():The trace_remove() method replaces trace_vdelete() method and it is used to unregister an observer. It returns the name of the callback used while registering the observer through trace_add() method initially.  
Syntax : trace_remove(self, mode, callback_name) Parameters: Mode: It is one of “array”, “read”, “write”, “unset”, or a list or tuple of such strings. callback_name: It is the name of the callback function to be registered on the tkinter variable.  
 trace_info(): The trace_info() method has replaced trace_vinfo() method and trace() method. It returns the name of the callback. This is generally used to find the name of the callback that is to be deleted. This method takes no argument other than the tkinter variable itself. 
 
trace_info(): The trace_info() method has replaced trace_vinfo() method and trace() method. It returns the name of the callback. This is generally used to find the name of the callback that is to be deleted. This method takes no argument other than the tkinter variable itself. 
Syntax : trace_info(self) 
 
 
To better understand the significance of the above methods let’s take an example and build a simple widget. Although using Tkinter variable as textvariable automatically updates the widget every time the variable changes but there might be times when the developer desires to do some extra processing while reading or modifying (or changing) the variable. This is where variable tracing comes in. Our callback function will trigger every time the text in the widget changes and will return a string “Variable Changed”. 
Python3
# Python program to trace# variable in tkinter  from tkinter import *  root = Tk() my_var = StringVar() # defining the callback function (observer)def my_callback(var, index, mode):    print ("Traced variable {}".format(my_var.get()) # registering the observermy_var.trace_add('write', my_callback) Label(root, textvariable = my_var).pack(padx = 5, pady = 5) Entry(root, textvariable = my_var).pack(padx = 5, pady = 5) root.mainloop()
Let’s see the code in action where the trace_add() method registers an observer which gets triggered every time the textvariable in the widget changes.Output: 
 
nnr223442
Python-gui
Python-tkinter
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
Selecting rows in pandas DataFrame based on conditions
How to drop one or multiple columns in Pandas Dataframe
Python | Get unique values from a list
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python Classes and Objects