in reply to can I call a tk fileevent with a delay
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: can I call a tk fileevent with a delay
by Anonymous Monk on Sep 13, 2013 at 05:38 UTC | |
by kcott (Archbishop) on Sep 14, 2013 at 12:01 UTC |
In Section
Seekers of Perl Wisdom