padawan_linuxero has asked for the wisdom of the Perl Monks concerning the following question:

Wise monks

I have being trying out Tk as a front end for a program I am working have learn a lot these days, but know I came to a problem that I really try to fix but just did not find how, you see I have a list of 4 Entry widgets all of them are created from a text file like this :
bank1|ACTIVE bank2|NO ACTIVE bank3|ACTIVE
and the program is this one:
sub make_entry1 { my ($upframe,$labe2,$filename1) = @_; my $label_info4 = $upframe->Label( -text => 'Status :', )->pack(-side => 'left', -expand => 1); open(DAT1, $filename1) || die("Could not open file!"); my @stat1=<DAT1>; close(DAT1); foreach my $stat1(@stat1) { chomp ($stat1); my $operacion; my $stado1; ($operacion,$stado1)=split(/\|/,$stat1); my $label_display1 = $upframe->Label( -textvariable => \$operacion )->pack(-side => 'left', -expand => 1); if ($stado1 eq "ACTIVO"){ my $entry_info1 = $upframe->Entry( -foreground => 'blue', -textvariable => \$stado1, -width => 20, )->pack(-side => 'left'); #-expand => 1); } else { my $entry_info1 = $upframe->Entry( -foreground => 'red', -textvariable => \$stado1, -width => 20, )->pack(-side => 'left'); #-expand => 1); } } }
As you can see I choose to create averything from the textfile becasue we add or take out a bank, but if you run this program you will see that at the end, that it repeat itself again and again , so my question is how to stop the widget entry from duplicating itself?
Thank you wise monks!

Replies are listed 'Best First'.
Re: Problem with duplicate Entry in One frame
by pc88mxer (Vicar) on May 19, 2008 at 19:14 UTC
    Sorry, I missed the my $operacion; declaration. Still I would advise you to use -text instead of -textvariable since you are not capturing the references to the variables anyway.

    Update: Another thing I would do is to make sure each line has the right format before processing it:

    foreach my $stat1(@stat1) { chomp ($stat1); my $operacion; my $stado1; ($operacion,$stado1)=split(/\|/,$stat1); next unless defined($stado1); ... }
    The next statement will skip blank lines and lines that do not contain a |-delimiter.
      Hi
      thanks, but my problem continues you see this is what is happenning on the screen :
      STATUS : BANK1 ACTIVE BANK2 ACTIVE BANK3 ACTIVE STATUS : BANK1 ACTI +VE....
      to check on the file I use repeat and I need to see the file every 10 seconds so every ten seconds it repeat it self the same line over end over again... is there a way to clean the frame and let it draw it again or something like that>??
Re: Problem with duplicate Entry in One frame
by pc88mxer (Vicar) on May 19, 2008 at 22:36 UTC
    Hi thanks, but my problem continues you see this is what is happenning on the screen :
    STATUS : BANK1 ACTIVE BANK2 ACTIVE BANK3 ACTIVE STATUS : BANK1 ACTIVE. +...
    What you really need to do then is to update the widgets that you've already created, and in order to do that you'll need to keep track of the which widgets are associated with which banks.
    my $upframe = ...; my %entry_widget; # stores the entry widget for a bank sub update_bank_status { my ($upframe, $bank, $status) = @_; my $entry = $entry_widget{$bank}; unless ($entry) { $entry_widget{$bank} = ...create a new label and entry widget... } $entry->configure(-text => $status); $entry->configure(-foreground => $status eq "ACTIVO" ? "blue" : "red"); } sub update_statuses { ...open file... while (<FILE>) { my ($bank, $status) = ...parse info...; update_bank_status($upframe, $bank, $status); } close(FILE); }
    And then you call update_statuses() every 10 seconds.
      Hi I follow your code
      but perhpas I am doing something wrong, can you tell me what is wrong?
      this is the code :
      sub update_bank_status { my ($upframe, $bank, $status) = @_; my $entry = $entry_widget{$bank}; unless ($entry) { $entry_widget{$bank} = $upframe -> Label ( -textvariable => \$bank )-> pack ( -side => 'left', -expand => 1 ); $upframe -> Entry ( - width => 20 )-> pack (-side => 'left', -expand => 1); } $entry -> configure (-text => $status); $entry -> configure (-foreground => $status eq "ACTIVO" ? "blue" : + "red" ); } sub update_statuses { open(FILE, "santa1.txt") || die("Could not open file!"); my @stat1=<FILE>; #close(FILE); while (<FILE>) { my ($bank, $status) = split(/\|/,@stat1); print $bank; print $status; update_bank_status ($upframe, $bank, $status); } close (FILE); }
      inside the mainloop I put this :
      $mw->repeat(5000, \&update_statuses);
      Thank you!!! I really apreciate the effort