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 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

#!/bin/sh
# -*- 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?

Thanks,
Manoz.

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 http://www.wellho.net/resources/ex.php4?item=t217/deferintk



This page is a thread posted to the opentalk forum at www.opentalk.org.uk 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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho