| |||||||||||
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/deferintkThis 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.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |