Hello Monks,

I'm searching fo guidance on add a feature to my recent Tk sparetime project. My probably naif understanding of the MainLoop is the mother of all errors. I'm seeking, if possible, explaination and examples.

Problem: I want to add an autoplay function to a secondary TopLevel window. In the below, semplified, example I display a sequence of chars A..Z A button on the primary window starts the autoplay function, and this works in the way I expected. But it turns out I'm not able to stop that autoplay. I imagined that checking a $toggle_autoplay variable was sufficient. I've bound the variable to the p key on the keyboard. It seems though that the variable remains unchanged (UPDATE: it seems the problem was an if?then:else related problem. the longer contruct below works...).

A)Is this the best way to implement an autoplay function? I tried to use Tk's repeat and timer with no big success. I read all tk callback material but if I understood it correctly a variable used in the callback it is evaluated when the callback is created NOT when it is invoked. How to change this behaviour?

B)why the ' ? : ' construct fails? Why it only works with an if(){}else{} one. Are not the same?

Background: the real application shows photos on the secondary window. I dont want to use modules like Tk::Slideshow or similar to accomplish the task. I want to do it on my own to understand better how MainLoop works.

The below code shows what i'm working on ()

#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Pane; use Tk qw(:eventtypes); my $mw = new MainWindow (-title=>" $0 MAIN"); my $phwin; my $display_label; my @to_show =('A'..'Z'); my $toggle_autoplay = 0; my $fr0 = $mw->Frame()->pack(); $fr0->Button( -text => "autoplay", -command => sub{$toggle_autoplay = 1; &autoplay}, #[\&autoplay, Ev $toggle_autoplay?$toggle_autoplay=0:$t +oggle_autoplay=1], )->pack(); # ok same behaviour of ->update #$mw->DoOneEvent(DONT_WAIT | ALL_EVENTS);# # the above without bitmask run but not at the right interval #also ok is: $mw->update; &secondary_win; MainLoop; ###################################################################### +########## sub autoplay { while($toggle_autoplay){ $display_label->configure(-text=> shift @to_show); $phwin->update; #$phwin->DoOneEvent; sleep 1; #last if $phwin->waitVariable(\$toggle_autoplay); } } ###################################################################### +########## sub secondary_win { # window does not exists if (! Exists($phwin)) { $phwin = $mw->Toplevel(); $phwin->title("SECONDARY"); $phwin->bind('<KeyRelease-p>' => sub { print "BEFORE: bind-p = $t +oggle_autoplay\n"; # THIS WORKS #if ($toggle_autoplay == 0 +){ # $toggle_autoplay = 1; #} #else{ # $toggle_autoplay = 0; #} # THE FOLLOWING DOES NOT W +ORKS! WHY? $toggle_autoplay == 0 ? $toggle_autoplay = 1 : $toggle_autoplay = 0 ; print "AFTER: bind-p = $to +ggle_autoplay\n"; }) } # window Exists else { $phwin->deiconify( ) if $phwin->state() eq 'iconic'; $phwin->raise( ) if $phwin->state() eq 'withdrawn'; } #my $scrolledframe = $phwin->Scrolled('Frame',-scrollbars => 'osoe +')->pack(); $display_label = $phwin->Label(-text=>'SECONDARY')->pack; }

Thanks in advance!

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.

In reply to 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.