How can I pass in parameters without calling a function in Python? [duplicate]

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How can I pass in parameters without calling a function in Python? [duplicate]



This question already has an answer here:



I am using the Tkinter bind function, and I would like to bind a button to a function. However, this function takes parameters (which, for obvious reasons, I have put in parentheses).


encrypt_button = Button(window, text='Encrypt')
encrypt_button.bind('<Button-1>', bl_encrypt(foo))



My problem is that, since I am passing 'foo' into my function, the function is called whenever the program gets to this line, not when I click on the button. This would not usually happen as, if there were no parameters to be passed, I would not put parentheses after the function name.



How do I bind to this function and pass parameters, but without the function being called as soon as I run the program?



My Python version is 3.6.0, if that helps.



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





I'm not that fluent with TkInter, but passing lambda like: encrypt_button.bind('<Button-1>', lambda: bl_encrypt(foo)) wouldn't help?
– Andrej Kesely
12 mins ago


encrypt_button.bind('<Button-1>', lambda: bl_encrypt(foo))





Use functools.partial or a lambda to create a callable with no arguments that calls bl_encrypt with foo.
– jonrsharpe
10 mins ago


functools.partial


lambda


bl_encrypt


foo





@AndrejKesely and jonrsharpe I have been looking into how I could use anonymous functions to solve my problem for ages but I did not think to do that! Thank you very much :) :) :)
– Ethan Wrightson
8 mins ago







@AndrejKesely Almost correct. A .bind callback receives the Event as a parameter, so you'd need something like lambda event: bl_encrypt(foo) or perhaps lambda event, original_foo=foo: bl_encrypt(original_foo), depending on how you want foo to be handled.
– PM 2Ring
6 mins ago




.bind


lambda event: bl_encrypt(foo)


lambda event, original_foo=foo: bl_encrypt(original_foo)


foo





There's lots of good info in the linked questions. It's probably worthwhile spending some time checking out the info there. Admittedly, some of that info may be a bit confusing, or too advanced at this stage, but it's ok to just skip over that stuff. ;)
– PM 2Ring
3 mins ago




hOiXSx,jStrbvxB29PQta9o,c Xm4 ecHFR,fFr4aQvgROXnPr4GJAr WdYr vdE,qt0022SC,CLYKwirC8GmpMS4jTJgc9tqetg dyR
wQ5LKx Ej hZFtO4,qNPtVZLB,6MC9T,MoV

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham