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).

1 Answers1

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'))
matszwecja
  • 6,357
  • 2
  • 10
  • 17
  • 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. – acw1668 Mar 03 '22 at 11:29
  • 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. – matszwecja Mar 03 '22 at 12:10