How to pass string var in callback function using trace() in tkinter

I am new with python and trying to create two entry widgets to evaluate the data as soon as the user typed in the entry box. How to pass the string var into a function callback so that it will return exactly which entry box has changed. Now the callback function just returns the first entry box (entry1).

Here is my code:

import tkinter
from tkinter import StringVar

main_wd = tkinter.Tk()
my_var1 = StringVar()
my_var2 = StringVar()

def my_callback(var):
    print("Traced variables {}".format(var.get())

my_var1.trace('write', my_callback(my_var1))
my_var2.trace('write', my_callback(my_var2))
entry1 = tkinter.Entry(main_wd, textvariable = my_var1).pack(padx = 5, pady = 5)
entry2 = tkinter.Entry(main_wd, textvariable = my_var2).pack(padx = 5, pady = 5)

main_wd.mainloop() 


Read more here: https://stackoverflow.com/questions/64894998/how-to-pass-string-var-in-callback-function-using-trace-in-tkinter

Content Attribution

This content was originally published by VanN at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: