2

I have a fairly simple Python problem which I really struggle to solve.

I have a LabelFrame widget within a Tk widget parent. The LabelFrame contains a number of widgets, including labels, entry widgets and buttons. Upon pressing a button, I want to execute a def, however want to send the user inputs in the entry widgets to the def for processing. However. when I add any parameters to the command in the button (e.g. command=make_new_user(entry1,entry2), the command executes automatically when running the code, without even pressing the button. I want the command only to execute when the button is pressed, but also be able to send the parameters to the processing def.

Can anyone help with this please?

Thanks, MiddleClassMan

1 Answer 1

1

That is how it was supposd to be - whenever you add parentheses after teh name of a function you are calling it. Using only its name, you are referencing the function object.

TKinter per se does not allow one to add parameters to a callback.

The usual thing to do is to create a "throw away function", using the "lambda" keyword - this throw away function receives no parameters itself - but it records inside itself the parameters you want to pass down to your real handler.

So, if the matter where only this, it would be the case of instead of writing:

Button(..., command=handler(par1, par2), ...)

write:

Button(..., command=(lambda: handler(par1, par2)), ...)

However there is one more thing in Python's nature that prevents this from working - if "par1" and "par2" are variables, their value is "live" - that means the parameters sent to the Button would be watherver values par1 and par2 where holding at the time the Button was clicked.

The way to avoid this is to add another level of indirection, using the lambda keyword, so that the parameter values are "frozen" at Button creation time.

For readbility sake, one better do that in 2 lines, instead of inlining it in the button creation call:

command = (lambda p1, p2: lambda: handler(p1, p2)) (par1, par2)
Button = (..., command=command, ... )

Doing it this way, the variable "command" will contain a throw away function in which the variables "p1" and "p2" have the instant value of "par1" and "par2"

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, I will try it out! Is this a common problem occurring? I am just a self-taught programmer, but I find this a bit limiting in Python...
This is more about "Tkinter" - the graphical toolkit than from Python. Tkinter comes bundled with Python in windows, but it is not the only graphical toolkit to choose from. And once one does understand this, it becomes easy to write a bunch of helper functions that automate this sort of things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.