Reputation: 55
I started Python(2.7.3) two days ago and I need to use parameters in my GUI callbacks functions. I've been looking for any information here and on effbot, but I cant find out what is wrong in my case.
from Tkinter import *
fenetre = Tk()
var_texte = StringVar()
ligne_texte = Entry(fenetre, textvariable=var_texte ,width=30)
def callback(s):
print("we got there with :"+s)
trace_name = var_texte.trace_variable("w",lambda: callback(ligne_texte.get()))
ligne_texte.pack()
fenetre.mainloop()
I tried to use lambda to get my param passed but i get this error when I type text into the Entry:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
return self.func(*args)
TypeError: <lambda>() takes no arguments (3 given)
I found a post here which proposed to replace lambda:
with lambda x:
, but I get another error with TypeError: <lambda>() takes exactly 1 argument (3 given)
Any explanation or source or information would be very appreciated :)
Upvotes: 0
Views: 2682
Reputation: 10667
As the error message says, StringVar.trace_variable
is called with three argument by the framework. So your lambda function should accept those three argument:
lambda name, idx, mode: ...
From http://staff.washington.edu/rowen/ROTKFolklore.html here is the description of the arguments:
Upvotes: 3
Reputation: 24788
If you are not interested in the parameters being passed to the callback, you can use:
lambda *args: callback(ligne_texte.get())
The documentation for the so-called "splat" operator (*
) can be found here.
Upvotes: 1