Python Forum
[Tkinter] tkinter best way to pass parameters to a function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] tkinter best way to pass parameters to a function (/thread-35548.html)



tkinter best way to pass parameters to a function - Pedroski55 - Nov-16-2021

I want to pass 2 numbers to a function in tkinter.

The numbers are in a list. The default values are numbers = [10, 2]

When I tried using partial(calcGrowth, numbers[0], numbers[1]) I always got the default values, even after I entered new values.

So I tried this with lambda, seems to work.

Is it the best way to pass the parameters to a function in tkinter?

btn5 = tk.Button(frame1, text='calculate exponent', command=lambda: calcGrowth(numbers[0], numbers[1]))
btn5.grid(columnspan=2, column=0, row=3, sticky='w', pady=10)



RE: tkinter best way to pass parameters to a function - deanhystad - Nov-16-2021

Whatever works is what's best.

The lambda expression works because it is essentially the same as doing this:
def lambdafunc():
    calcGrowth(numbers[0], numbers[1])

btn5 = tk.Button(frame1, text='calculate exponent', command=lambdafunc)
When you press the button you don't call calcGrowth, you call the function created by the lambda expression. This function calls calcGrowth, but as always the arguments are evaluated before the function gets called.

When you want the arguments to change from call to call this is a good thing. Usually this is not the desired behavior and you need to modify the lambda expression to call your function with static values. Something like this:
btn5 = tk.Button(frame1, text='calculate exponent', command=lambda a=numbers[0], b=numbers[1]: calcGrowth(a, b)
In this example the lambda expression creates a function with arguments. For arguments sake lets say numbers[0] is 42 and numbers[1] is 3.14.
def lambdafunc(a=42, b=3.14):
    calcGrowth(a, b)

btn5 = tk.Button(frame1, text='calculate exponent', command=lambdafunc)
partial provides a way to do the same thing with a cleaner syntax and without the extra function


RE: tkinter best way to pass parameters to a function - Pedroski55 - Nov-16-2021

Thanks for your reply!

I try to avoid passing arguments directly by storing them in lists, which are available to all functions, but sometimes, it seems unavoidable.

Is there any other way of passing variable parameters to functions in tkinter??

Like I said, partial read the default values at creation time and reflected those in the function. I was surprised.


RE: tkinter best way to pass parameters to a function - deanhystad - Nov-17-2021

Partial did not "read" anything. You created a partial with the arguments passed. The "reading" happened before the partial was created. If you passed a mutable object the object value might be different each time the partial executes, but it will always pass the same objects as arguments to the bound function.