Tkinter menu commands activate when menu is built
Matthew Dixon Cowles
matt at mondoinfo.com
Fri Nov 9 22:23:48 EST 2001
On Fri, 9 Nov 2001 20:17:43 -0500, David Mallwitz
<dmallwitz at cox.rr.com> wrote:
Dear Dave,
>According to the documentation
>menu.add_command(label='x',command=y)
>should create a dropdown menu entry named 'x' that will activate
>function y() when clicked.
[. . .]
>connectmenu.add_command(label="Example 1", command=do_stuff(1))
Compare the exmple to your code again <wink>. You're not giving it a
command=y, you're giving it a command=y(). The first is a matter of
passing a function around, the second is calling the function.
Everyone get bitten by this early on in writing Tkinter programs.
The simple thing is to use four functions but I bet that you wanted to
use that argument for a reason, probably because the code would be
more natural with one function rather than four. In that case, you can
store the state in an object that will respond to being called.
Something like this would probably work for you:
class do_stuff:
def __init__(self,val):
self.val=val
return None
def __call__(self):
print "My stored value is",self.val
return None
Then you could say command=do_stuff(1).
Regards,
Matt
More information about the Python-list
mailing list