in reply to Tk starting and stopping an autoplay loop using a keybinding
Here's a version using ->after() for the timing. (You should never sleep() in a Tk program.)
Some things are different, for one, my windowmanager (RicksWM) does not have icons, so I left that part out
and just destroyed the secondary window instead.
#!/usr/bin/perl # http://perlmonks.org/?node_id=1173140 use strict; use warnings; use Tk; my @to_show = 'A'..'Z'; my $toggle_autoplay = 0; my $show = 'SECONDARY'; my $top; my $mw = new MainWindow; $mw->geometry('+400+0'); $mw->Button( -text => 'Autoplay', -command => \&click, -font => 'courierbold 100', )->pack; MainLoop; sub advance { if( $toggle_autoplay ) { $show = shift @to_show; push @to_show, $show; $mw->after(1000, \&advance); } } sub click { if( $toggle_autoplay ) { $toggle_autoplay = 0; $top->destroy; } else { $top = $mw->Toplevel; $top->title('SECONDARY'); $top->bind('<KeyRelease-p>' => \&click ); $toggle_autoplay = 1; $top->Label( -textvariable => \$show, -font => 'courierbold 200', -width => 2, )->pack; advance(); } }
Your problem with ?: is actually explained in perlop.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Tk starting and stopping an autoplay loop using a keybinding
by Discipulus (Canon) on Oct 04, 2016 at 09:46 UTC |