The Python Oracle

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

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Secret Catacombs

--

Chapters
00:00 Using The Lambda Function In 'Command = ' From Tkinter.
01:13 Accepted Answer Score 9
01:37 Answer 2 Score 0
01:55 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()))