in reply to Multiple frames

When creating the button, you know what frame to delete. There are two ways how to pass parameters to callbacks: you can use a closure or use an array reference:
# Closure - BROKEN! $frame[$a] -> Button(-text => "Delete frame", -command => sub { rmv( $ +frame[$a]) })->pack; # Array ref $frame[$a] -> Button(-text => "Delete frame", -command => [ \&rmv, $ +frame[$a] ] )->pack; } sub rmv { shift->destroy; }

Update: Oops, closures do not work for global arrays. Fix:

for (0 .. 5) { my $frame = $fmain->Frame(-borderwidth => 0)->pack(-side => 'left' +); $frame->Label( -text => "Frame " . \$whichframe[$_] )->pack; $frame->Button(-text => "Delete frame", -command => sub { rmv($frame) }, )->pack; }
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Multiple frames
by delmejo (Initiate) on Aug 27, 2013 at 22:17 UTC
    Thank you!
      ok, so now i have added an entry widget to all the frames and using an array to store the values. it is always empty.
      #!/usr/bin/perl -d use Tk; $fmain = MainWindow -> new; $fmain -> title("Frame delete test"); @mycolor = qw(red green blue violet yellow black); for ( 0 .. 5 ) { my $frame = $fmain->Frame(-borderwidth => 10, -relief => 'groove', -b +ackground => $mycolor[$_]) ->pack(-side => 'top'); $lbl = $frame -> Label( -text => "Enter username: " ) ->pack; $ent = $frame -> Entry( -text => $username[$_] ) ->pack; $btn = $frame -> Button(-text => "Delete frame # " . $_, -command => sub { $val = $ent->cget('-text'); rmv_frm($frame,$val); } ) ->pack; } $ex = $fmain -> Button(-text => "Exit",-command => sub { exit } ) -> pack; MainLoop; sub rmv_frm { shift -> destroy; #$val_ref = $ent->cget('-text'); #$val = $$val_ref; $val = shift; print "You entered: $val\n"; # if no frames exist, need to "withdraw" fmain. $fmain -> withdraw if $fmain -> children == 0;
        **Better format** ok, so now i have added an entry widget to all the frames and using an array to store the values. it is always empty.
        #!/usr/bin/perl -d use Tk; $fmain = MainWindow -> new; $fmain -> title("Frame delete test"); @mycolor = qw(red green blue violet yellow black); for ( 0 .. 5 ) { my $frame = $fmain->Frame(-borderwidth => 10, -relief => 'groove', -b +ackground => $mycolor[$_]) ->pack(-side => 'top'); $lbl = $frame -> Label( -text => "Enter username: " ) ->pack; $ent = $frame -> Entry( -text => $username[$_] ) ->pack; $btn = $frame -> Button(-text => "Delete frame # " . $_, -command => sub { $val = $ent->cget('-text'); rmv_frm($frame,$val); } ) ->pack; } $ex = $fmain -> Button(-text => "Exit",-command => sub { exit } ) -> pack; MainLoop; sub rmv_frm { shift -> destroy; #$val_ref = $ent->cget('-text'); #$val = $$val_ref; $val = shift; print "You entered: $val\n"; # if no frames exist, need to "withdraw" fmain. $fmain -> withdraw if $fmain -> children == 0; }
        Just wondering if anyone has an idea about this? Its driving me crazy. Thanks in advance.
        Oops. I just re-read my post.. Sorry. I'm curious why I get an empty variable when I push the delete button. I use an array in the entry widget bc devices usually have different usernames. I know I'm not passing it correctly. I'm not sure how to reference the correct array after pushing the button.