python - Why is Button parameter "command" executed when declared? -
this question has answer here:
i'm new python , trying write program tkinter. why hello-function below executed? understand it, callback executed when button pressed? confused...
>>> def hello(): print("hi there!") >>> hi=button(frame,text="hello",command=hello()) hi there! >>>
it called while parameters button being assigned:
command=hello() if want pass function (not it's returned value) should instead:
command=hello in general function_name function object, function_name() whatever function returns. see if helps further:
>>> def func(): ... return 'hello' ... >>> type(func) <type 'function'> >>> type(func()) <type 'str'> if want pass arguments, can use lambda expression construct parameterless callable.
>>> hi=button(frame, text="hello", command=lambda: goodnight("moon")) simply put, because goodnight("moon") in lambda, won't execute right away, instead waiting until button clicked.
Comments
Post a Comment