You can read perldoc Tk::callbacks, but it really dosn't explain the simple principle. When you create the callback, you want it to be evaluated "at a later time", not "when it is created". You may run into this as an hard to track down bug, where Tk programs will crash on startup, with weird error messages. It usually is a button command being evaluated as the program starts up. Here are a couple of examples to illustrate the fine points.
#!/usr/bin/perl my @b; for my $i ( 1 .. 5 ) { $b[$i] = $top->Button( -text=>$i, -command=> &mybuttons($i), ^^^^^^^^^^^^^^^^ WRONG # Stephen O. Lidie writes: # -command expects a *code reference*, that shall, at some future ti +me, # execute. You've called for an immediate execution, which in theor +y could # return a code ref, but doesn't here. # Ya'all all are skrting around a closure, which may be created in v +arious # ways, all detailed in you know what. # Try: -command => sub {&mybuttons($i)} #or maybe: -command => [\&mybuttons, $i]

or

#!/usr/bin/perl -w use Tk; my $xx=99; sub a{ my $x=$_[0]; my $wm= new MainWindow; my $l=$wm->Entry(-textvariable => \$x)->pack(); $wm->Button(-text => "Apply", # -command => [ \&onApply, "$x"])->pack(); -command => [ \&onApply, \$x])->pack(); } # Here, you specify an anonymous list as the value of the -command opt +ion, # which is a legal callback syntax. The problem is that this list will + be # created when the button is created, and the first element in the lis +t is # the reference to onApply(), but the second element is the *value* of + $x # at the time the list is created, which is the initial value of $x. T +he # variable $x itself has nothing to do with it anymore, so changing it + # will not change this anonymous list. # What you can do is pass a reference to $x instead: # -command => [\&onApply, \$x], # and change your onApply() sub to reflect that. sub onApply{ # my $z=$_[0]; my $z = ${$_[0]}; print "XX=$z\n"; }; &a($xx); MainLoop();

I'm not really a human, but I play one on earth. flash japh

In reply to Re: Subs attached to Tk Buttons by zentara
in thread Subs attached to Tk Buttons by Scarborough

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.