in reply to Re: Multiple frames
in thread Multiple frames

Thank you!

Replies are listed 'Best First'.
Re^3: Multiple frames
by delmejo (Initiate) on Aug 28, 2013 at 17:18 UTC
    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.

        Just wondering if anyone has an idea about this? Its driving me crazy. Thanks in advance

        Ideas about what? You said

        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.

        so what is the problem, the same one as in the OP?

      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.

        Basically , you're not using the right method "get", and variable scoping (what choroba said, closures don't work for globals). Compare your program to this (the commented lines are equivalent in effect)

        I avoid closures by using references

        #!/usr/bin/perl -- use strict; use warnings; use Tk; use Data::Dump qw/ dd /; Main( @ARGV ); exit( 0 ); sub Main { GoTk(); GoTk(); } sub GoTk { my $mw = tkinit( -title => "Frame delete test" ); my @mycolors = qw(red green blue violet yellow black); my @username_refs; for my $ix ( 0 .. 5 ) { my $username = ""; my $frame = $mw->Frame( -borderwidth => 10, -relief => 'groove', -background => $mycolors[$ix], )->pack; my $lbl = $frame->Label( -text => "Enter username: " )->pack; my $ent = $frame->Entry( -textvariable => \$username )->pack; my $btn = $frame->Button( -text => "Delete frame # " . $ix, #~ -command => [ $frame, 'destroy' ] -command => \&delete_parent_frame )->pack; push @username_refs, \$username; } my $exit = $mw -> Button( -text => "Exit", -command => [ $mw, 'destroy' ], )-> pack; $mw->$_ for qw/ withdraw deiconify raise focusForce update /; MainLoop; #~ print map { "You entered $$_\n" } @username_refs; dd\@username_refs; } ## end sub GoTk sub delete_parent_frame { my $button = $Tk::event->W; my $frame = $button->parent; #~ my $text = ( $frame->children )[1]; #~ my $text = ( grep { $_->isa('Tk::Entry') } $frame->children +)[0]; my( $text ) = grep { $_->isa( 'Tk::Entry' ) } $frame->children; #~ my $textvar = $text->cget( '-text' ); ## WEIRD! my $textvar = $text->cget( '-textvariable' ); print "You entered $textvar : $$textvar\n"; my $textstr = $text->get; print "You did entered $textstr\n"; $frame->destroy; return; } ## end sub delete_parent_frame

        Read the tutorials in links in readmore in Re: Entry Edit Refresh (nested subs and closures)

        Also, Hi, perlmonks has threaded discussions :) so click the reply link of the node you wish to reply to -- otherwise you're talking to yourself :) which coincidentally is a pretty good debugging technique :) 1 / 2