Tkinter is a popular Python GUI library that provides developers with a range of widgets and tools to create desktop applications with ease. One of the useful features that Tkinter offers is the ability to trace variables, i.e., track changes to a variable and execute a callback function whenever the variable is written, read, or unset.
This tutorial will introduce you to the trace() method in Tkinter and show you how to use it to trace variables, register callbacks for changes in variables, and remove traces when required.
Writing a Callback Function for the “write” Mode
The “write” mode is used to detect when a variable is written to, i.e., when its value changes. In the following example, we create an Entry widget that binds to a StringVar named “var”. We then add a trace to “var” with the “write” mode and register a callback function that prints the mode of the trace.
import tkinter as tk
root = tk.Tk()
def write_callback(var, index, mode):
print("The variable has been written to in mode:", mode)
var = tk.StringVar()
entry_widget = tk.Entry(root, textvariable=var)
entry_widget.pack(padx = 50, pady = 50)
var.trace_add("write", callback= write_callback)
root.mainloop()
The callback function takes three arguments:
var
: the variable that has changed.index
: the index of the first element affected by the change.mode
: the mode of the trace.
The trace_add()
method adds a new trace to the variable with the specified mode and callback function.
Writing a Callback Function for the “read” Mode
The “read” mode is used to detect when a variable is read, i.e., when its value is accessed. In the following example, we create an Entry widget that binds to a StringVar named “var”. We then add a trace to “var” with the “read” mode and register a callback function that prints the mode of the trace.
In this example, we add a trace to “var” with the “read” mode and register a callback function that prints the mode of the trace. The callback function takes the same three arguments as the write_callback()
function.
We also create a button widget that calls the var.get()
method to access the value of the variable and trigger the callback function. The “read” action is triggered whenever we perform a read operation on the var
variable, such as the get()
method.
import tkinter as tk
root = tk.Tk()
def read_callback(var, index, mode):
print("The variable has been read in mode:", mode)
def click():
var.get()
var = tk.StringVar()
entry_widget = tk.Entry(root, textvariable=var)
entry_widget.pack(padx = 50, pady = 50)
var.trace_add("read", callback= read_callback)
tk.Button(root, text = "click me", command = click).pack()
root.mainloop()
Writing a Callback Function for the “unset” Mode
The “unset” mode is used to detect when a variable is unset, i.e., when its value is deleted. In the following example, we create a function named func() that creates a new StringVar named “var” and adds a trace to “var” with the “unset” mode. We then call this function to add the trace and register a callback function that prints the mode of the trace.
import tkinter as tk
root = tk.Tk()
def unset_callback(var, index, mode):
print("The variable has been unset in mode:", mode)
def func():
var = tk.StringVar()
var.trace_add("unset", callback= unset_callback)
func()
root.mainloop()
The unset callback is triggered here after the execution of func()
, as the local variable var
(declared within a function) gets destroyed after the function ends.
Removing a Trace
Sometimes, you may want to remove a trace that you previously added. This can be done using the trace_remove()
method, which takes the same arguments as trace_add()
: the mode, the callback function, and the trace ID.
Here’s an example of how to remove a trace:
import tkinter as tk
root = tk.Tk()
def write_callback(var, index, mode):
print("The variable has been write in mode:", mode)
def click():
var.trace_remove("write", tid)
var = tk.StringVar()
tid = var.trace_add("write", callback= write_callback)
entry_widget = tk.Entry(root, textvariable=var)
entry_widget.pack(padx = 50, pady = 50)
tk.Button(root, text = "click me", command = click).pack()
root.mainloop()
In this example, we first create a trace using trace_add()
and save the trace ID in a variable tid
. We then create an entry widget and a button that, when clicked, will remove the trace.
The click()
function is called when the button is clicked. It calls trace_remove()
with the mode and the trace ID, effectively removing the trace.
This marks the end of the “How to use trace() in Tkinter” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.