By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,681 Members | 1,864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,681 IT Pros & Developers. It's quick & easy.

TkInter buttons - callback (command) function

P: 2
It seems that in some cases the callback (command) function in a TkInter Button runs automatically when drawn for the first time. Example:
b=Button(frame, text="Click me",command=myFunction())
b.pack()
..
so, myFunction() executes when the screen is drawn for the first time.
But (!) if I do not include the "()" after myFunction, then it does not run until it is explicitly clicked. IE:
b=Button(frame, text="Click me", command=myFunction)
..
In this case myFunction does not run until clicked.
Is this correct behavior? It seems strange to me, and I do not find it documented anywhere.
(I am using a very recent version of python/tkinter on XP)
Thanks!
Erasmus
Sep 11 '07 #1
Share this Question
Share on Google+
2 Replies


P: 2
oops. I see whats going on .. obviously: Python calls the function and returns the result to command. So is there any way to send parameters directly in a callback function?

It seems that in some cases the callback (command) function in a TkInter Button runs automatically when drawn for the first time. Example:
b=Button(frame, text="Click me",command=myFunction())
b.pack()
..
so, myFunction() executes when the screen is drawn for the first time.
But (!) if I do not include the "()" after myFunction, then it does not run until it is explicitly clicked. IE:
b=Button(frame, text="Click me", command=myFunction)
..
In this case myFunction does not run until clicked.
Is this correct behavior? It seems strange to me, and I do not find it documented anywhere.
(I am using a very recent version of python/tkinter on XP)
Thanks!
Erasmus
Sep 11 '07 #2

ilikepython
Expert 100+
P: 844
oops. I see whats going on .. obviously: Python calls the function and returns the result to command. So is there any way to send parameters directly in a callback function?
Do you mean like this:?
Expand|Select|Wrap|Line Numbers
  1. but = Button(text = "Press", command = lambda x = "blah": myFunction(x))
  2.  
Notice that the function doesn't get called right away, it is in the body of the lambda.
Sep 11 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.