Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Entry Edit Refresh

by PilotinControl (Scribe)
on Aug 26, 2013 at 18:34 UTC ( #1050991=perlquestion: print w/ replies, xml ) Need Help??
PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

Good Afternoon Monks I have another question thanks again in advance. When editing I can successfully edit and save changes and once the file is viewed it shows the changes were successful. If I try and open the file again and try ti edit it will not let me. Here are my thoughts. 1.) The file never gets closed although close $fh is used the program has to be completely shut down and restarted to edit again. 2.) The text entry never gets updated/refreshed in order to edit the file again. I also need to add when this is ran as a standalone script it works just fine. Thoughts and suggestions thanks!

sub editcar { my $DATAFILE = 'cardata.txt'; my $mw = tkinit( -title => 'Edit Cars' ); $mw->geometry( '560x380+100+100' ); $mw->group($mw); $mw->resizable(0,0); $mw->protocol('WM_DELETE_WINDOW' => sub { print "do nothing here\n"; #prevents destruction of $mw #by WM control }); my $balloon = $mw->Balloon(); my $ec = $mw->Scrolled('GridColumns' => -scrollbars => 'ose', -data => \my @data, -columns => \my @columns, -itemattr => { -bg => Tk::NORMAL_BG }, -itemgrid => { -padx => 1, -pady => 1 }, -item_bindings => { '<Double-ButtonPress-1>' => \&edit_item }, )->pack(-fill => 'both',-expand => 1)->Subwidget( 'scrolled' ); @columns = ( {-text => 'MAKE', -command => $ec->sort_cmd( 0, 'abc' ),}, {-text => 'MODEL', -command => $ec->sort_cmd( 1, 'abc' ),}, {-text => 'COLOR', -command => $ec->sort_cmd( 2, 'abc' ),}, {-text => 'YEAR', -command => $ec->sort_cmd( 3, 'num' ),}, ); open_editcar( $ec, $DATAFILE )->refresh; my $frm_bottom = $mw->Frame->pack(-side => 'left',-fill => 'x'); my $frm_bottom2 = $mw->Frame->pack(-side => 'left',-fill => 'x'); $frm_bottom->Button(-text => 'SAVE CHANGES',-command => sub { save_edi +tcar( $ec, $DATAFILE ), sleep 2; $mw->withdraw; $rm->deiconify;} )->p +ack(-side => 'left'); $balloon->attach($frm_bottom, -balloonmsg => "Save Changes to Cars"); $frm_bottom2->Button(-activebackground => 'red', -activeforeground => +'white', -text => "CLOSE", -command => sub { $mw->withdraw; $rm->deic +onify;})-> pack(-side => 'left'); $balloon->attach($frm_bottom2, -balloonmsg => "Close Edit Cars"); sub open_editcar { my( $ec, $file ) = @_; my $csv = Text::CSV->new({ 'binary' => 1 }); my @data; open my $fh, '<', $file or die "Cannot open file '$file': $!\n"; while ( my $line = <$fh> ) { chomp $line; next if $line =~ /^\s*(?:#.*)?$/; $csv->parse( $line ); my @fields = split( /\:/, $line); if ( defined $fields[0] ) { if ( @fields == 6 ) { push @data, \@fields; } else { warn "open_editcar() warning: not enough fields\n$file:$.: $line\n"; } } else { my @error = $csv->error_diag; die "open_editcar() failure $error[0]: $error[1]:\n$file:$.:$error[2]: + ". $csv->error_input ."\n"; } } close $fh; @{ $ec->data } = @data; return $ec; } # open_editcar sub save_editcar { my( $ec, $file ) = @_; my $csv = Text::CSV->new({ 'binary' => 1, 'always_quote' => 0, sep_cha +r => ':' }); open my $fh, '>', $file or die "Cannot open file '$file': $!\n"; for my $row ( 0 .. $#{ $ec->data } ) { $csv->combine( @{ $ec->data->[$row] } ); my $line = $csv->string; if ( defined $line ) { print $fh "$line\n"; } # if else { my @error = $csv->error_diag; die "save_editcar() failure $error[0]: $error[1]:\ndata row $row: ". $ +csv->error_input ."\n"; } # else } # for close $fh; return $ec; } # save_edit car *edit_item = sub { my($self, $w, $row, $col) = @_; $w->destroy; my $entry = $self->Entry(-textvariable => \$data[$row][$col],-width => + 0)->grid(-row => $row+1,-column => $col,-sticky => 'nsew'); $entry->selectionRange( 0, 'end' ); $entry->focus; $entry->bind( '<Return>' => sub { $self->refresh_items } ); $entry->bind( '<FocusOut>' => sub { $self->refresh_items } ); } # end edit car } # END CAR

Comment on Entry Edit Refresh
Download Code
Re: Entry Edit Refresh (nested subs and closures)
by Anonymous Monk on Aug 27, 2013 at 00:52 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (21)
As of 2014-08-27 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (239 votes), past polls