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
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems

Phone System reconfiguration 11.1.2018 to 26.1.2018 - we are on email but incoming landline not available. Temp number 0797 4 925928
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., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho