I have set up an Entry widget using tkinter and am using a StringVar in order to trace any changes to it. However, when I try to use the callback function, I find that I am unable to pass any arguments to it.
Here is what I have tried:
hpstring = StringVar()
hpstring.trace("w", hptrace)
hpstring.set("0")
hpbox = tk.Entry(frame, textvariable=hpstring)
hpbox.pack()
def hptrace(*args):
print(hpstring)
This fails because hpstring is not defined.
hpstring = StringVar()
hpstring.trace("w", hptrace(hpstring))
hpstring.set("0")
hpbox = tk.Entry(frame, textvariable=hpstring)
hpbox.pack()
def hptrace(*args):
print(hpstring)
This also somehow fails.
hpstring = StringVar()
hpstring.trace("w", lambda hpstring: hptrace(hpstring))
hpstring.set("0")
hpbox = tk.Entry(frame, textvariable=hpstring)
hpbox.pack()
def hptrace(*args):
print(hpstring)
This time, it fails because "() takes 1 positional argument but 3 were given".
Is there any way to have my callback function (hptrace) take hpstring as an argument?