[Tutor] Tkinter-Button class

Kent Johnson kent37 at tds.net
Mon Mar 19 12:13:09 CET 2007


ammar azif wrote:
> Hi,
> 
> Thanks for answering .. btw what do you mean by explicit , helper 
> function? Can you explain about these functions?

I just mean, define an ordinary function of no arguments that does what 
you want. For example, the calculator program I linked to has this Button:
tk.Button(root,text='1',width=5,relief=tk.RIDGE,
   command=lambda: click('1'))

That could be written as
def click1():
   click('1')
tk.Button(root,text='1',width=5,relief=tk.RIDGE, command=click1)

Notice there are no parentheses after 'click1' in the Button definition; 
you are passing the actual function to the Button.

Kent

PS Please reply on-list.

> 
> */Kent Johnson <kent37 at tds.net>/* wrote:
> 
>     ammar azif wrote:
>      > Hi,
>      >
>      > Thanks for the help guys.
>      >
>      >
>      > I have tried gui programming using Tkinter and use the Button class
>      > which accepts the command argument which is a function object.
>      >
>      > The question is how to send arguments if the function accepts
>     arguments.
> 
>     A common way to do this is to use a lambda expression to bind the
>     arguments, e.g.
>     Button(..., command = lambda: func_with_args(1, 2, 3))
> 
>     For example see
>     http://www.daniweb.com/code/snippet610.html
> 
>     You could also define explicit, short helper functions if you don't
>     want
>     to use lambdas.
> 
>     Kent
> 
> 
> ------------------------------------------------------------------------
> Don't pick lemons.
> See all the new 2007 cars 
> <http://autos.yahoo.com/new_cars.html;_ylc=X3oDMTE0OGRsc3F2BF9TAzk3MTA3MDc2BHNlYwNtYWlsdGFncwRzbGsDbmV3Y2Fycw--> 
> at Yahoo! Autos. 
> <http://autos.yahoo.com/new_cars.html;_ylc=X3oDMTE0OGRsc3F2BF9TAzk3MTA3MDc2BHNlYwNtYWlsdGFncwRzbGsDbmV3Y2Fycw--> 
> 



More information about the Tutor mailing list