Tkinter menu commands activate when menu is built

Greg Krohn volucris at hotmail.com
Fri Nov 9 22:34:05 EST 2001


'command' needs to be a assigned a function, not the value returned from the
function (in this case None). Here lambda is you friend. try this instead:

connectmenu.add_command(label="Example 1", command=lambda n=1: do_stuff(n))
connectmenu.add_command(label="Example 2", command=lambda n=2: do_stuff(n))
connectmenu.add_command(label="Example 3", command=lambda n=3: do_stuff(n))
connectmenu.add_command(label="Example 4", command=lambda n=4: do_stuff(n))

or even:

for num in range(1, 5):
    connectmenu.add_command(label="Example %d" % num, command=lambda n=num:
do_stuff(n))


greg





More information about the Python-list mailing list