Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I'd use Tk::after - it has millisecond granularity.

While I appreciate that the code you posted may well be a highly simplified version of your real application, you may not need Tk::fileevent at all. See my example code where it wasn't needed.

#!/usr/bin/env perl use strict; use warnings; use Tk; my $mw = MainWindow->new(); my $action_F = $mw->Frame()->pack(-side => 'bottom'); $action_F->Button(-text => 'Exit', -command => sub { exit })->pack; my $text_F = $mw->Frame()->pack(-fill => 'both', -expand => 1); my $ls_T = $text_F->Scrolled('Text', -scrollbars => 'osoe', -wrap => ' +none'); $ls_T->pack(-fill => 'both', -expand => 1); my $out_win = $ls_T; my $tid; my $cmd = 'ls -al 2>&1'; my $repeat = 6; my $delay = 100; $tid = $mw->repeat($delay => [\&update_ls, \$out_win, \$tid, \$cmd, \$ +repeat]); MainLoop; { my $updates = 0; sub update_ls { my ($text_ref, $tid_ref, $cmd_ref, $repeat_ref) = @_; $$text_ref->insert(end => scalar(localtime) . "\n" . qx{$$cmd_ +ref}); if (++$updates >= $$repeat_ref) { my $times_format = '(Usr: %d, Sys: %d, ChUsr: %d, ChSys: % +d)'; $$text_ref->insert(end => sprintf $times_format => times); $$tid_ref->cancel; } $$text_ref->yview('end'); return; } }

Notes:

  • From Tk::after, I've used the repeat() method to set it up; and the cancel() method when the callback's been called 6 times. [Beyond mirroring the "for my $i ( 0 .. 5) ..." (in your posted code), the 6 is not special.]
  • Notice how all the callback arguments are references. This gives you a lot more flexibility: you could, for instance, change the delay, the command, or even the Tk::Text widget displaying the output.
  • The defaults for -width and -height are 80 and 24, respectively: I've left those two out. I've made the -scrollbars optional (osoe).
  • The code I've posted is complete, working and tested on a directory with 412 entries.

-- Ken


In reply to Re: can I call a tk fileevent with a delay by kcott
in thread can I call a tk fileevent with a delay by Anonymous Monk

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (7)
    As of 2014-07-10 03:29 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (198 votes), past polls