Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

A ghost Tk Entry in my program

by padawan_linuxero (Scribe)
on May 10, 2008 at 17:02 UTC ( [id://685895]=perlquestion: print w/replies, xml ) Need Help??

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!!!!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://685895]
Approved by pc88mxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2026-03-16 12:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.