0

I've utilized the lambda function to call two different functions (read_input_data(), which reads data a and b from GUI, and run, which takes the data a and b and makes some computations) after a Tkinter button is pressed

start_button = tk.Button(text = 'Start', command = lambda:[ [a,b] = read_input_data(), run(a,b)], bg = 'firebrick', fg = 'white', font = ('helvetica', 9, 'bold'))

However, it returns a syntax error. If I remove the outputs [a,b] from read_input_data(), the syntax becomes correct but my code won't work as I need a and b to execute the second function run(a,b).

3
  • 1
    Use a normal function instead of lambda. Commented Mar 3, 2022 at 10:12
  • How did you come up with this [] notation? Commented Mar 3, 2022 at 10:12
  • Or lambda: run(*read_input_data()). Commented Mar 3, 2022 at 10:14

1 Answer 1

1

Lambdas are just a shorthand notation, functionally not much different than an ordinary function defined using def, other than allowing only a single expression

x = lambda a, b: a + b

is really equivalent to

def x(a, b):
    return a + b

In the same way, what you tried to do would be no different than doing:

def x():
    a, b = read_input_data()
    run(a, b)
start_button = tk.Button(text = 'Start', command = x, bg = 'firebrick', fg = 'white', font = ('helvetica', 9, 'bold'))
Sign up to request clarification or add additional context in comments.

2 Comments

OP lambda does not have arguments, so your answer is not an answer to OP question. Also command=x expects x function does not have arguments. I wonder why OP accepts this as an answer.
Maybe because even if it does not work, it shows enough to make adapting the code very straightforward. Also OP's example wasn't really clear in terms of what he is trying to do due to weird use of []. Anyway I've edited the answer to adress your issues.

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.