Python Forum
[Tkinter] tkinter best way to pass parameters to a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] tkinter best way to pass parameters to a function
#1
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)
Reply
#2
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
Pedroski55 likes this post
Reply
#3
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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pass a variable between tkinter and toplevel windows janeik 10 2,374 Jan-24-2024, 06:44 AM
Last Post: Liliana
  Using Tkinter inside function not working Ensaimadeta 5 5,085 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  Tkinter won't run my simple function AthertonH 6 3,903 May-03-2022, 02:33 PM
Last Post: deanhystad
  how to add two numbers and pass the result to the next page in tkinter? pymn 7 4,362 Feb-15-2022, 04:40 AM
Last Post: pymn
  Creating a function interrupt button tkinter AnotherSam 2 5,565 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,040 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter get function finndude 2 2,971 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,473 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  function in new window (tkinter) Dale22 7 5,190 Nov-24-2020, 11:28 PM
Last Post: Dale22
Star [Tkinter] How to perform math function in different page of Tkinter GUI ravaru 2 4,604 Oct-23-2020, 05:46 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020