Tkinter menu commands activate when menu is built
Brad Bollenbach
bbollenbach at home.com
Fri Nov 9 22:18:20 EST 2001
[snippity snip snip]
> This is my first attempt at a program using Tkinter. I'm ok with the
> widgets, geometry managers, frames, etc... but the menu bar is giving me
> grief.
But it's doing what it should. :)
> 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.
Precisely.
[snip]
> The problem is that the function
> do_stuff() executes when the menu is built (in the program below, it will
> execute 4 times as the root window is being built) rather than when the
> widget is clicked on.
> Am I missing something basic here?
Yes! :)
[snip irrelevant code]
> connectmenu.add_command(label="Example 1", command=do_stuff(1))
[snip more irrelevant code]
You think you're saying "when Example 1 is clicked, execute do_stuff(1)".
What you're actually doing here is calling do_stuff(1) and assigning the
result (if any) to the keyword parameter "command".
The right way to do this would be:
connectmenu.add_command(label="Example 1", command=do_stuff)
passing the function reference do_stuff (instead of actually calling it
right then and there), so that Tkinter knows what to call when "Example 1"
is clicked.
> Thanks,
> Dave
Hope that helps,
Brad
More information about the Python-list
mailing list