Tkinter trace() method callback Parameters
Tkinter variable trace() using r mode Mentioned:
def callbackW(*args):
-
Actually, three parameters are passed, which are
- tk variable name
- index
- mode
However, the part about tk variable names and index indexes has not yet completed the actual support, as for the third parameter, it can be listed as r or w mode. Since the program we are designing does not need to pass parameters, we can just use *args
as the parameter content.
Tkinter trace() method callback Parameters Example#1
Lists the parameters inside the callback() method called by the trace() method.
from tkinter import *
def callbackW(name,index,mode): # Executed when the content is changed
xL.set(xE.get()) # Change label content
print("name = %r, index = %r, mode = %r" % (name,index,mode))
root = Tk()
root.title("apidemos.com")
xE = StringVar()
entry = Entry(root,textvariable=xE)
entry.pack(pady=5,padx=10)
xE.trace("w",callbackW) # If there is a change execute callbackW
xL = StringVar() # Label's variable content
label = Label(root,textvariable=xL)
xL.set("Synchronized display")
label.pack(pady=5,padx=10)
root.mainloop()
Output:
The following execution results can be seen in the Python Shell window: