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.


In reply to Re: Tk starting and stopping an autoplay loop using a keybinding by tybalt89
in thread Tk starting and stopping an autoplay loop using a keybinding by Discipulus

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.