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

Hi Monks, I have a CSV file which contains two fields Emp_ID and Emp_Name. I have a form with a display button. When i click on this button all the information from the file should get displayed in Label and Entry widgets which gets displayed dynamically (as there may be n no of records in the csv file, might be less than 25). I hope you ppl can help me out.

Replies are listed 'Best First'.
Re: retriving info from a csv file in Tk
by lwicks (Friar) on May 20, 2008 at 12:26 UTC
    If it is the CSV parsing cauusing the problem the following might help:

    require Text::CSV; my $csv = Text::CSV->new; open( FH, "<$fh" ) || die "Error: no open for $fh: $!"; foreach (<FH>) { if ( $csv->parse($_) ) { my @field = $csv->fields; my ( $Emp_ID, $Emp_Name ) = @field; push(@id, $Emp_ID); push(@names, $Emp_Name); } } # end foreach close(FH);
    Assuming everything else in place the above code should read a file and give you two arrays, one of names one of IDs.

    More esteemed monks can I am sure point out better ways of doing this, I'm sure. I justy wated to give an example of Text::CSV for you to look at.

    Lance

    Kia Kaha, Kia Toa, Kia Manawanui!
    Be Strong, Be Brave, Be perservering!

Re: retriving info from a csv file in Tk
by zentara (Cardinal) on May 20, 2008 at 13:09 UTC
    And once you use lwicks method to extract your csv to an array, just use the following sample code, to load them into a Tk::Pane. I just use 1..13, but you will want to cycle thru your array.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Pane; my $mw =MainWindow-> new (-title => "Demo"); $mw->geometry('340x300'); my $frame_tl= $mw->Scrolled('Pane', -scrollbars => 'se', )->pack(-expand => 1, -fill => 'both'); my %f; for (1..13){ $f{$_}{'frame'}= $frame_tl->Frame(-borderwidth =>2, -relief=> 'groo +ve') ->pack(-side => 'top', -fill => 'x'); $f{$_}{'label'} = $f{$_}{'frame'}->Label(-relief => 'groove',-text= +>" $_ :" ) ->pack(-side => 'left', -fill => 'y'); $f{$_}{'entry'}= $f{$_}{'frame'}->Entry(-takefocus => 1,-width => 6 +0, -textvariable=>\$_) ->pack(-side => 'left', -fill => 'y'); } MainLoop;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Hi lwicks and zentara, Thank you for answering my query. Can you please explain me how to delete the record frm the pane. Say i want to delete the record of employee whose EMP_Name = 'ABC'. I am not getting the reference to that particular widget. Pls let me know how to do the same.
Re: retriving info from a csv file in Tk
by apl (Monsignor) on May 20, 2008 at 12:25 UTC
    As a brother Monk would say, "it's obviously caused by your line 42".

    Without the source for the minimal program that demonstrates this problem, there's little hope of anyone being able to help you.

Re: retriving info from a csv file in Tk
by marto (Cardinal) on May 20, 2008 at 12:02 UTC
    Perhaps I am misunderstanding your question, I am not sure exactly what part you are stuck it. Is it purely how to display this information within a Tk Window? You could take a look at Tk::Table or Tk::GridColumn.

    Hope this helps

    Martin
Re: retriving info from a csv file in Tk
by zentara (Cardinal) on May 22, 2008 at 13:38 UTC
    Here is one way, but if you want to delete from a list, you may be better off going with a Listbox widget. Do you really need a Label and Entry side by side? This example could be made to select multiple checkboxes first, then delete them all at once. OR, you could work out a mouse binding to do the delete, instead of a checkbox.
    #!/usr/bin/perl # only moderately tested !!!!!! use warnings; use strict; use Tk; use Tk::Pane; use Data::Dumper; my %hash; while (<DATA>) { chomp; if(length $_){ ( my ( $key, $val ) = split /,/ ); $hash{$key} = $val; } } #print Dumper(\%hash); my $mw =MainWindow-> new (-title => "Demo"); $mw->geometry('340x300'); my $frame_tl= $mw->Scrolled('Pane', -scrollbars => 'se', )->pack(-expand => 1, -fill => 'both'); my %f; for my $key (sort keys %hash){ $f{$key}{'frame'}= $frame_tl->Frame(-borderwidth =>2, -relief=> 'gr +oove') ->pack(-side => 'top', -fill => 'x'); $f{$key}{'checkbox'}= $f{$key}{'frame'}->Checkbutton( -text=> $key, -command=> sub{ &del_it($key) }, )->pack(-side => 'left', -fill => 'y'); $f{$key}{'label'} = $f{$key}{'frame'}->Label(-relief => 'groove',-t +ext=>" $key " ) ->pack(-side => 'left', -fill => 'y'); $f{$key}{'entry'}= $f{$key}{'frame'}->Entry(-takefocus => 1,-width +=> 60, -textvariable=> \$hash{$key}) ->pack(-side => 'left', -fill => 'y'); } MainLoop; sub del_it{ my $key = shift; my $frame = $f{$key}{'frame'}; my @w = $frame->packSlaves; foreach (@w) { $_->packForget; } $frame->packForget; $frame_tl->update; $mw->update; } __DATA__ 123,ABC 345,CDE 546,MDJ 987,ASD 543,kjf 937,dhy 098,sdf 867,mnb 321,jhg 567,fgh 834,ctg 912,xcv

    You might be able to use a listbox easier. A simple example.

    #!/usr/bin/perl use strict; use Tk; my $selected_text; my $mw = tkinit; my $lb = $mw->Scrolled( "Listbox", -scrollbars => "e", -selectmode => "single", )->pack; $mw->Label( -textvariable => \$selected_text )->pack; $lb->insert( 'end', qw/w2200 w2465 W2475 W2420/ ); $lb->bind( '<Button-1>', sub { # I don't use an array because -selectmode eq 'single' my $selection_index = $lb->curselection(); if ( $selection_index eq '' ) { $selected_text = "Nothing selected"; } else { $selected_text = $lb->get($selection_index); $lb->delete($selection_index); } } ); MainLoop;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum