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

Dear monks
I come with a question, seeking knowledge :), I being working
with Tk because I need it a graphic way to input data and display data too,
so right now I am working with Entry I am displaying data from a file to the Entry, the thing is that I added color to it, if it file has ACTIVE then it will be blue, when in the file has NON ACTIVE is red, but now I have a problem well look at the code :
sub Status_bancos { #Banamex########################################################## +######################### my $etiqueta_info1 = $upperframe->Label( -text => 'Santander :', )->pack(-side => 'left', -expand => 1); my $archivo1 = "santa1.txt"; open(DAT0, $archivo1) || die("Could not open file!"); my @stat0=<DAT0>; close(DAT0); foreach my $stat0(@stat0) { chomp ($stat0); my $banco0; my $stado0; ($banco0,$stado0)=split(/\|/,$stat0); if ($stado0 eq "ACTIVO"){ my $entry_info0 = $upperframe->Entry( -foreground => 'blue', -textvariable => \$stado0, -width => 20, )->pack(-side => 'left', -expand => 1); }else{ my $entry_info0 = $upperframe->Entry( -foreground => 'red', -textvariable => \$stado0, -width => 20, )->pack(-side => 'left', -expand => 1); } } my $label_info1 = $upperframe->Label( -text => 'Banamex :', )->pack(-side => 'left', -expand => 1); my $filename1 = "banamex.txt"; open(DAT1, $filename1) || die("Could not open file!"); my @stat1=<DAT1>; close(DAT1); foreach my $stat1(@stat1) { chomp ($stat1); my $banco1; my $stado1; ($banco1,$stado1)=split(/\|/,$stat1); if ($stado1 eq "ACTIVO"){ my $entry_info1 = $upperframe->Entry( -foreground => 'blue', -textvariable => \$stado1, -width => 20, )->pack(-side => 'left', -expand => 1); } else { my $entry_info1 = $upperframe->Entry( -foreground => 'red', -textvariable => \$stado1, -width => 20, )->pack(-side => 'left', -expand => 1); } } #HSBC################################################################# +################### my $label_info2 = $upperframe->Label( -text => 'HSBC :', )->pack(-side => 'left', -expand => 1); #---Extraccion de los datos de el archivo my $filename2 = "hsbc.txt"; open(DAT2, $filename2) || die ("Could not open file"); my @stathsbc=<DAT2>; close(DAT2); foreach my $statushsbc (@stathsbc) { chomp($statushsbc); my $banco2; my $stado2; ($banco2,$stado2)=split(/\|/,$statushsbc); if ($stado2 eq "ACTIVO"){ my $entry_info2 = $upperframe->Entry( -foreground => 'blue', -textvariable => \$stado2, -width => 20, )->pack(-side => 'left', -expand => 1); } else { my $entry_info2 = $upperframe->Entry( -foreground => 'red', -textvariable => \$stado2, -width => 20, )->pack(-side => 'left', -expand => 1); } } #Banorte############################################################## +############# my $label_info3 = $upperframe->Label( -text => 'Banorte :', )->pack(-side => 'left', -expand => 1); my $filename3 = "banorte.txt"; open(DAT3, $filename3) || die("Could not open file!"); my @stat3=<DAT3>; close(DAT3); foreach my $stat3(@stat3) { chomp ($stat3); my $banco3; my $stado3; ($banco3,$stado3)=split(/\|/,$stat3); if ($stado3 eq "ACTIVO"){ my $entry_info3 = $upperframe->Entry( -foreground => 'blue', -textvariable => \$stado3, -width => 20, )->pack(-side => 'left', -expand => 1); } else { my $entry_info3 = $upperframe->Entry( -foreground => 'red', -textvariable => \$stado3, -width => 20, )->pack(-side => 'left', -expand => 1); } } }
as you can see I have 8 Entries but visible there suppose to be only 4 whether is RED o BLUE, but every time I run it is displaying 5!!!!, the first one is the one that display 2 entries and I have type it, copy it but still same problem
Am I doing something wrong please help is very frustrating

Replies are listed 'Best First'.
Re: A ghost Tk Entry in my program
by pc88mxer (Vicar) on May 10, 2008 at 17:36 UTC
    For each of the four banks, you are creating one Entry widget for each line in the bank's associated file. So if one of the bank files (like banorte.txt) has two lines in it, there will be two Entry widgets for Banorte. Is that what you are seeing?

    Additionally, here are some other suggestions:

    1. You have four blocks of essentially identical code. You should factor the common code out into a subroutine:
      sub make_entry { my ($upperframe, $label, $filename) = @_; ... } make_entry($upperframe, 'Santander', "santa1.txt"); make_entry($upperframe, 'Banamex', "banamex.txt"); make_entry($upperframe, 'HSBC', "hsbc.txt"); make_entry($upperframe, 'Banorte', "banorte.txt");
      This will make your code a lot easier to debug and maintain.
    2. You can change the foreground color of an Entry widget after you create it, and this will further simplify your code:
      my $entry = $upperframe->Entry(-textvariable => ...)->pack(...); $entry->configure(foreground => ...bank is active... ? 'red' : 'blue +');
      This also shows how to dynamically change the foreground.
    3. When creating the Entry I'd probably just use -text => ... instead of -textvariable => \$somevar. You are creating a reference to a lexical that you won't be able to refer to later once the subroutine returns. You can always get the contents of the Entry widget with $entry->get.
      Thanks I see what you meant I did not know that about Entry and the file thanks

      I really aprecciate it thanks!!!!!!