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

Hello, I'd like to display the contents of a file in a TK listbox. The contents of the listbox should be refreshed automatically after n seconds. I saw the method Tk::After but I don't know how to use it. Maybe somebody can help me with the syntax or even knows other methods for the automatic refresh. Here is the code:
#!/usr/bin/perl use Tk; use Tie::File; use Tk::after; my $liste; my $liste_font; my $breite=100; ### Anzahl der abgezeigten Zeichen in der Liste my $the_selectmode = "extended"; ### "single","multiple","extended" my $enter; my @array_file; my $mw = MainWindow->new(); ### rahmen fuer Hauptseite my $frame1 = $mw->Frame(-width=>50, -height=>50, -bg=>"seashell"); my $frame2 = $mw->Frame(-width=>5, -height=>5, -bg=>"grey80"); $liste_font = $mw->fontCreate(-family=>"courier", -size=>7 ); +### zB treffer-Liste my $liste = $frame1->ScrlListbox( ##-font=>$liste_font, -setgrid=>1, -scrollbars=>"se", #-background=>"wheat3", -background=>"lemonchiffon3", -borderwidth=>3, -highlightthickness=>10, ##-selectmode => "extended", ###"multiple" ##-selectmode => "multiple", ##-selectmode => "single", ## -selectmode =>$the_selectmode, -height => 30, ## -width => $breite, -selectforeground=>"blue", -selectbackground=>"green", ##-setgrid=>1, ##-selectborderwidth=>1, -relief=>"ridge", -exportselection => 1)->pack(-side=>"right", -expand=> +1, -fill=>"both"); my $exitButton = $frame2->Button ( -text=>"Schliessen" ,-command=> +"exit" ,-bg=>"red" ,-activebackground=>"red" ,-activeforeground=>"cya +n" )->pack(-anchor=>"w" ,-padx=>10 ,-pady=>15 ,-ipady=>10 ,-fill=>" +x"); ################################################################## ### Packen der Rahmen auf Hauptseite ############################ ################################################################## $frame1->pack(-side => 'left' ,-expand=>1 ,-fill=>"both"); $frame2->pack(-side => 'right',-expand=>1 ); $frame2->pack(-expand=>1 ,-fill=>"both"); ############################################## ### sofort ausgefuehrte Subroutines ############################################## =pod while(1) { #&fill_from_file(); sleep(2); }; =cut #&fill_from_file(); &fill_with_tie(); ############################################## ### Ende sofort ausgefuehrte Subroutines ############################################## MainLoop; ###################################################### sub fill_from_file { my $file = "meinfile.txt"; my $line; $liste->delete(0,"end"); if ( -s $file ) { open(DATEI, "<$file") or die $!; while ($line = <DATEI>) { chomp $line; $liste->insert(0,$line); } close(DATEI); } ## if -s $file } ## fill_from_file ################# ############################################################ ###################################################### sub fill_with_tie { my $file = "meinfile.txt"; my $line; my $elem; $liste->delete(0,"end"); if ( -e $file ) { tie @array_file, "Tie::File", $file || die $!; foreach $elem (@array_file) { chomp $elem; $liste->insert(0,$elem); } ## foreach untie @array_file; } else { print "Kann $file nicht oeffnen $!\n"; } ## if -s $file } ## fill_with_tie ################# ############################################################

Replies are listed 'Best First'.
Re: listbox refresh with Tk::After
by Khen1950fx (Canon) on Jul 28, 2007 at 00:45 UTC
    I'd put in a repeat like this:
    #!/usr/bin/perl use strict; use warnings; use Tk; use Tie::File; use Tk::After; #... my $liste = $frame1->ScrlListBox( #... )->pack( -side => "right", -expand => 1, -fill => "both" ); $liste->repeat( 5000, \&fill_from_file );
Re: listbox refresh with Tk::After
by zentara (Cardinal) on Jul 28, 2007 at 11:07 UTC
    Yeah, the Tk::after and Tk::repeat modules can be a source of confusion for a beginner. There is no perldoc for Tk::repeat, yet it is what is most often used. If you read perldoc Tk::after, you will see
    $id = Tk::After->new($widget, tid, $time, ’once’, callback); $id = Tk::After->new($widget, tid, $time, ’repeat’, callback); $id->cancel; once or repeat specifies whether the timer is a one-time-after event, or a repeating repeat event.
    It just became a shortcut to write
    $id = $mw->repeat( 10, sub{} );

    As far as your script goes, you might want to checkout the listbox -textvariable option to tie an array

    #!/usr/bin/perl -w use strict; use Tk; my $mw = Tk::MainWindow->new( -title => 'Listbox' ); my @array = ('A'..'Z'); my $lb = $mw->Scrolled('Listbox', -listvariable => \@array, )->pack(); my $b = $mw->Button( -text => 'Delete B', -command => sub{ del_it('B') }, )->pack(); MainLoop; sub del_it { my $remove = shift; @array = grep { $_ ne 'B' } @array; } ##################################################################
    that way, you can just load your array from the file
    open FH, '< myfile' or warn "$!\n" ; my (@array) = <FS>; close FH; $listbox->update; # may not be needed

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