Welcome
Welcome to dinksoftware

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!

Tkinter >> Callbacks and lambda

Holiverh's izHawt Python....

Tkinter >> Callbacks and lambda

Postby Holiverh » Sat Aug 15, 2009 10:11 am

To kick off the Python forum i start with a peice of knowledge i only picked up the other day after the spending hours wrestling with different peices of code trying to get it to work. But bare in mind, i do not fully understand why this works myself however i'm currently reading more into it.

Using the same function more than once whilst passing arguments in Tk is not as simple as it seems. Many people --including myself-- start with something like this.
Code: Select all
#This code ommiits many formalities.
Button(frame,command=self.foo(x)).grid()

def foo(y):
    print "I like " + str(y)
The above will not work.
Code: Select all
#This code ommits many formalities.
Button(frame,command=lambda: self.foo(x)).grid()

def foo(y):
    print "I like " + str(y)
Using the built in lambda statement you're able to pass arguments with your functions.

Thread Revisited: Entire months have gone by since i posted this, and now i feel very n00bish. :lol: The reason the lambda statement is required, is because when Tk needs to fire a callback, it calls the __call__() method on the object passed to it via the "command" option. By using something like "self.foo(x)", you have to remind yourself that it is essentially representing the returned value from that call. Example:
Code: Select all
# Formalities omitted

def foo(x): return x + 10

my_button = tk.Button(parent_widget, command=foo(10))
Is essentially:
Code: Select all
my_button = tk.Button(parent_widget, command=20)
The "20" is an integer object, which does not have a __call__() method bound to it, so when Tk tries to execute it, it raises "TypeError: 'Object' not callable.".

To remedy this, we utilize lambda. From my understanding, lambda can be simply put as a "function not bound to a name"; importantly this means it returns an object which has the shiny __call__() method! This means we can wrap out "foo(10)" inside a lambda, which will return the object which Tk will call, which will in turn call foo(10). Tada, you've got your callback!

* Okay, it's not a sophisticated answer, but it'll do right? :P
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

 

Re: Tk tricks! >> Commands and lambda

Postby Dink » Sat Aug 15, 2009 10:20 am

I don't understand it either, perhaps you could start with a few more basic tips and tutorials for us noobs lol :P.

:ugeek:
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia

Re: Tk tricks! >> Commands and lambda

Postby Holiverh » Sat Aug 15, 2009 11:08 am

Dink wrote:I don't understand it either, perhaps you could start with a few more basic tips and tutorials for us noobs lol :P.

:ugeek:
Yep, i totally agree with you, but i thought i'd cover a wide variety of things, from the basics to UI creation. What sort of basics would you like teaching? :lol: I am hoping we could all help each other understand things better.
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

Re: Tk tricks! >> Commands and lambda

Postby Dink » Sat Aug 15, 2009 11:12 am

Python 101, from the ground up... perhaps throw in homework exercises as well etc to complete... :ugeek:
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia

Re: Tk tricks! >> Commands and lambda

Postby Unknown98 » Sat Aug 15, 2009 11:51 am

ahh my eyes I don't understand anything lol.
http://www.salesmangame.com
We have an IRC channel! #Salesman on coldfront
User avatar
Unknown98
 
Posts: 97
Joined: Sun Jul 12, 2009 11:30 am
Location: Houston

Re: Tk tricks! >> Commands and lambda

Postby Holiverh » Sat Aug 15, 2009 12:05 pm

So you want me to start with 'print' statements?
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

Re: Tk tricks! >> Commands and lambda

Postby Dink » Sat Aug 15, 2009 12:11 pm

Holiverh wrote:So you want me to start with 'print' statements?


Yes, if you could start with the basics it would be helpful. :ugeek:
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia

Re: Tkinter >> Callbacks and lambda

Postby Holiverh » Sat Mar 27, 2010 6:52 am

I revisited this thread, and updated my original post with a proper (Well, a better...) explanation of why the use of lambdas are required with Tk callbacks.
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

Re: Tkinter >> Callbacks and lambda

Postby Dink » Thu Jun 17, 2010 8:15 pm

So you're basically calling the function with the parameter by value?
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia

Re: Tkinter >> Callbacks and lambda

Postby Holiverh » Fri Jun 18, 2010 1:03 am

Dink wrote:So you're basically calling the function with the parameter by value?

Basically: If you set the -command option to "foo()" the interpreter will execute the function and pass the returned value from "foo()" as an argument instead of the function it self.

We wrap it in a "lambda" (unamed function) which returns a callable object which calls "foo()".

Any clearer?
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

Next

Return to Python

Who is online

Users browsing this forum: No registered users and 0 guests

cron
suspicion-preferred