Home > database >  Returning values in lambda function while using Tkinter button
Returning values in lambda function while using Tkinter button

Time:03-03

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

CodePudding user response:

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'))
  • Related