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

In reply to listbox refresh with Tk::After by hudo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.