471,350 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

TkInter buttons - callback (command) 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 #1
2 6363
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
844 Expert 512MB
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.

Similar topics

5 posts views Thread by Andrew Koenig | last post: by
2 posts views Thread by mksql | last post: by
8 posts views Thread by dakman | last post: by
3 posts views Thread by Jay | last post: by
1 post views Thread by Michael Yanowitz | last post: by
6 posts views Thread by Eric_Dexter | last post: by
2 posts views Thread by Doran, Harold | last post: by
3 posts views Thread by J-Burns | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.