Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Tk starting and stopping an autoplay loop using a keybinding

by tybalt89 (Monsignor)
on Oct 03, 2016 at 18:39 UTC ( [id://1173171]=note: print w/replies, xml ) Need Help??


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
    thanks tybalt89 for you working example: it turned out i was looking at the wrong part of Mastering Perl Tk book (chapter 13.22. Time Delays contains useful informations: i was on Anatomy of MainLoop).

    My problem with ?: was the worst Perl gaffe by me since years..

    You should never sleep() in a Tk program. I suspected this, and sounds like a good motto. It reminds me to an older one dormitare cogitantem de fuga vetat that translates more or less in: to sleep is forbidden to those plaining an escape

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1173171]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found