Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Tk problem with binding menus

Posted by nazgul (nazgul), 2 June 2005
Hi experts,

I'm facing a small problem with adding popup menus using tk_popup in a foreach loop.

The foolowing piece of code creates 3 labels and attaches a popup menu to each of it.

# -*- tcl -*- \
exec wish "$0" "$@"
set global_vars {rusty ocean danny}

set i 0
foreach var $global_vars {
# Create a menu

set m [menu .$var]
$m add check -label "Example 1" -variable check_$var  -command bell
$m add command -label "Example 2" -command bell

# Create something to attach it to
pack [label .l_$i -text $var]

# Arrange for the menu to pop up when the label is clicked
bind .l_$i <1> {tk_popup $m %X %Y}
incr i

But, the popup menu attached for all the three labels is same. When I check the checkbox of one popup menu other checkboxes are also checked.

I think the problem can be because of...
1. binding is not doene properly. The final popup menu is bound to all the labels.
2. The popup menu's check variable "check_$var" is executed when the checkbox is checked. So, the value of $var then will be last one (ocean).

Can you please help me in finding a solution?


Posted by admin (Graham Ellis), 2 June 2005
As a first quick glance, it looks as if you're binding a deferred block using { and } so that the value of $m is taken at EVENT time and NOT at the time you are binding to the event.  I think it will work if you replace the { and } with " and " - so:

bind .l_$i <1> "tk_popup $m %X %Y"

Posted by admin (Graham Ellis), 2 June 2005
P.S. There's an example showing the "wrong" and "right" of this on our web site at

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho