The Python Oracle

Using the lambda function in 'command = ' from Tkinter.

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC G Dvoks String Quartet No 12 Ame 2

--

Chapters
00:00 Question
01:59 Accepted answer (Score 8)
02:28 Answer 2 (Score 0)
02:54 Thank you

--

Full question
https://stackoverflow.com/questions/2877...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #lambda #tkinter

#avk47



ACCEPTED ANSWER

Score 9


The command lambda does not take any arguments at all; furthermore there is no evt that you can catch. A lambda can refer to variables outside it; this is called a closure. Thus your button code should be:

bouton1 = Button(main_window, text="Enter",
    command = lambda: get(Current_Weight, entree1))

And your get should say:

def get(loot, entree):
    loot = float(entree.get())
    print(loot)



ANSWER 2

Score 0


Actually, you just need the Entry object entree1 as the lamda pass-in argument. Either statement below would work.

bouton1 = Button(main_window, text="Enter", command=lambda x = entree1: get(x))
bouton1 = Button(main_window, text="Enter", command=lambda : get(entree1))

with the function get defined as

def get(entree):
    print(float(entree.get()))