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
#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


Messages In This Thread
RE: tkinter best way to pass parameters to a function - by deanhystad - Nov-16-2021, 10:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pass a variable between tkinter and toplevel windows janeik 10 2,746 Jan-24-2024, 06:44 AM
Last Post: Liliana
  Using Tkinter inside function not working Ensaimadeta 5 5,280 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  Tkinter won't run my simple function AthertonH 6 4,124 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,493 Feb-15-2022, 04:40 AM
Last Post: pymn
  Creating a function interrupt button tkinter AnotherSam 2 5,705 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,168 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter get function finndude 2 3,056 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,737 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  function in new window (tkinter) Dale22 7 5,417 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,687 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