[Tutor] tkinter and menu

BELSEY, Dylan dylan.belsey@baesystems.com
Thu, 17 Oct 2002 07:30:34 +0930


	Personally, lambdas more often than not cause me to break out in a
rash.  I'm not saying they are a bad thing, its just that I haven't invested
enough time to try and work them out.  So as an alternative I use a Command
class, which has been mentioned before on this list, to perform the same
function.  The link is:

http://mail.python.org/pipermail/python-list/2001-February/031420.html

	It allows one to pass in parameters to a function etc.

		Dylan



-----Original Message-----
From: alan.gauld@bt.com [mailto:alan.gauld@bt.com]
Sent: Thursday, 17 October 2002 01:55
To: lists@shrestha.net.np; tutor@python.org
Subject: RE: [Tutor] tkinter and menu


> This works fine, but i've modified my callback function help so that
> depending upon the paramter it displays help on given paramter.
> 
> def help(topic):
>   """ depending upont topic display help"""
>   statements
>       ..
>   statements
>      
> My question is how do i pass parameter to callback function help() ??
> doing .add_command(label="ok", command=help("about")) doesn't work.

There are a few ways to do it - one is to use bind() 
instead of 'command=' then pass a custom event, pesonally 
I find that messy and prefer to use a lambda(**) in 
the command= section.

def helpHandler(context):
   # ...do it here...

Button(parent,command=lambda ctx=globalContext: helpHandler(ctx))

This makes the variable globalContext the default value for the

lambda which then passes it to the helpHandler. That works 
provided the context is different for each Button you define.

If you need to pass the context dynamically (and I think in 
this case you do) then its a case of defining a global variable 
which you update with current context and then access from 
within helpHandler. Not good programming practice but about all 
you can do...

Of course if your program is using classes it becomes better 
coz the context variable becomes a class variable which tidies 
things up a bit!

(**)
If you aren't familiar with lambda take a look at the 
Functional programming topic in my online tutor

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor