binding label event to checkbox 
Author Message
 binding label event to checkbox

Hi All,

I have created a table of images with checkboxes between them. What I
need to do is to toggle the
checkbox when the user clicks on the image label. The code for the table
is below:

$table = $mw->Table( -columns => 5,
                                     -rows => 5,
                                     -scrollbars => 'sw',
                                     -relief => 'raised');

 for $i ( 0 .. 5 ) {
  for $j ( 0 .. 5 ) {
   $frame = $table->Frame(-relief => 'groove',
                                          -borderwidth => 2);
   $image = $frame->Photo('imggif',
                                              -file => "foo");
   $Imagelabel = $frame->Label(-image => $image);
   $Imagelabel->pack(-side => "top");
   $SelectCheckButton = $frame->Checkbutton(-text => "Shot $shotNum",

-variable => \$SelectArray[$i+$j],

-onvalue => 1,

-offvalue => 0);
   $SelectCheckButton->pack(-side => "top");
   $frame->pack();
   $table->put( $i, $j, $frame );
  }
 }

My guesses were along the lines of
  $Imagelabel ->bind('<ButtonPress-1>', sub{ ... });

The problem is that I cannot determine the index of te image clicked so
I cannot determine which element of SelectArray[] to toggle. Any help
will be much appreciated.

Thanks,

Cuneyt



Sun, 30 Jan 2005 09:32:38 GMT  
 binding label event to checkbox

Quote:
> Hi All,

> I have created a table of images with checkboxes between them. What I
> need to do is to toggle the
> checkbox when the user clicks on the image label. The code for the table
> is below:

> $table = $mw->Table( -columns => 5,
>                                      -rows => 5,
>                                      -scrollbars => 'sw',
>                                      -relief => 'raised');

>  for $i ( 0 .. 5 ) {
>   for $j ( 0 .. 5 ) {
>    $frame = $table->Frame(-relief => 'groove',
>                                           -borderwidth => 2);
>    $image = $frame->Photo('imggif',
>                                               -file => "foo");
>    $Imagelabel = $frame->Label(-image => $image);
>    $Imagelabel->pack(-side => "top");
>    $SelectCheckButton = $frame->Checkbutton(-text => "Shot $shotNum",

> -variable => \$SelectArray[$i+$j],

> -onvalue => 1,

> -offvalue => 0);
>    $SelectCheckButton->pack(-side => "top");
>    $frame->pack();
>    $table->put( $i, $j, $frame );
>   }
>  }

> My guesses were along the lines of
>   $Imagelabel ->bind('<ButtonPress-1>', sub{ ... });

Almost, but closures are your enemy here..

Quote:

> The problem is that I cannot determine the index of te image clicked so
> I cannot determine which element of SelectArray[] to toggle. Any help
> will be much appreciated.

Firstly you are adding $i and $j to become an array index. This means that you
will only have 10 values in the array minimum 0+0 and maximum 5+5, but you will
have 36 boxes! i.e. you are overwriting array values as you go. I suggest using

widget creation.

I have code a fully working example (not pretty - but functional) ! I didn't use
an image - but of course the same method work work for the image label
##################################
use Tk;
use Tk::Table;

my $mw=tkinit;
my %SelectHash;

$table = $mw->Table( -columns => 5,
                     -rows => 5,
                     -scrollbars => 'sw',
                     -relief => 'raised')->pack;

 for $i ( 0 .. 5 ) {
  for $j ( 0 .. 5 ) {
   my $frame = $table->Frame(-relief => 'groove',
                                          -borderwidth => 2);

   my $Imagelabel = $frame->Label(-text => "Row $i Col $j");
   $Imagelabel->pack(-side => "top");
   $Imagelabel->bind('<ButtonPress-1>', [\&toggle,$i,$j]);
   $frame->Checkbutton(-text => "Check $i $j",
      -variable => \$SelectHash{"R${i}C${j}"},
      -onvalue => 1,
      -offvalue => 0)->pack;
   $frame->pack();
   $table->put( $i, $j, $frame );
  }
 }
MainLoop;

sub toggle
{


($SelectHash{"R${r}C${c}"})?($SelectHash{"R${r}C${c}"}=0):($SelectHash{"R${r}C${
c}"}=1);

Quote:
}

Jack


Sun, 30 Jan 2005 11:57:18 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Problem with binding events on TkPerl canvas

2. Binding resize event

3. How to bind 'Back Space' event

4. Binding mouse event to a canvas

5. Binding event to entry widget

6. binding an event to X

7. Ignoring key repeat events with bindings.

8. What is the resize event to bind to?

9. Trying to bind subroutine to event.

10. example code for binding, and handling visibilty events?

11. Bind event for Window resize?

12. scrolled event/bind help!

 

 
Powered by phpBB® Forum Software