2

Im new at programming and have no clue how to use lambda in programming. Im asking if there is a way to edit this code so that I dont need to use lambda on it.

from tkinter import *
def define(a):
    pass

root = Tk()

#this line
auto = Button(root, text="auto", command=lambda: define(True)).pack()
2
  • I don't think this programming is running either way because the define function isn't taking any arguments although one is given as define(True) Commented Feb 13, 2021 at 11:49
  • 1
    create a function that doesn't take any parameter then use command=somFun now call other functions from someFun or you can also use functools.partial Commented Feb 13, 2021 at 11:54

2 Answers 2

3

If you want to remove lambda from your code you must define a function return another function define.

That is,

def replacement():
    return define(argument)

This way you don't have to use lambda in calling Button. Instead you would call Button as :

auto = Button(root, text="auto", command=replacement).pack()

I hope this helps. But, you should learn what lambda's are.

Sign up to request clarification or add additional context in comments.

3 Comments

This will execute the function even before the button is pressed.
Thanks for pointing that out. I have edited. Please have a look again.
return define(argument) should be return define(True) because argument is undefined.
2

You can also use partial from functools. It is basically the same as what @Suraj Upadhyay suggested. This is the code:

from tkinter import *
from functools import partial

def define(a):
    pass

root = Tk()

function = partial(define, True) # The first arg is the function name and the rest are the function args
auto = Button(root, text="auto", command=function)
auto.pack()

1 Comment

you could just use command=partial(define, True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.