Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Subs attached to Tk Buttons

by Scarborough (Hermit)
on Aug 20, 2004 at 11:07 UTC ( [id://384578]=perlquestion: print w/replies, xml ) Need Help??

Scarborough has asked for the wisdom of the Perl Monks concerning the following question:

I've looked for ages for a way to pass parameters in Tk, even an answer to an early question I posted seems wrong.
Whats the right way of doing this
$widget->Button(-text=>"mybutton", -command=>\$mysub("a1","a2");

Thanks for your help I expect the answer is stareing in the face somewhere.

Replies are listed 'Best First'.
Re: Subs attached to Tk Buttons
by Mr_Jon (Monk) on Aug 20, 2004 at 12:21 UTC
    You need to pass the callback and its arguments in an array reference as follows, where the first argument is a ref to the callback, followed by the subroutine arguments themselves:
    $widget->Button(-text=>"mybutton", -command=> [ \&mysub, 'a1', 'a2' ])

    There's a nice section on this in Mastering Perl/TK.
Re: Subs attached to Tk Buttons
by zentara (Archbishop) on Aug 20, 2004 at 14:31 UTC
    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
Re: Subs attached to Tk Buttons
by rlb3 (Deacon) on Aug 20, 2004 at 12:31 UTC
    Hello,
    I think you can send arguments this way:

     $widget->Button(-text=>"mybutton", -command=>[\$mysub,"a1","a2"];

    You can find the docs here.

    rlb3
Re: Subs attached to Tk Buttons
by fletcher_the_dog (Friar) on Aug 20, 2004 at 16:39 UTC
    Another way to do it besides what others have already told you is to use a closure
    $widget->Button(-text=>"mybutton",-command=>sub { mysub("a1","a2",@_) +};
Re: Subs attached to Tk Buttons
by davidj (Priest) on Aug 20, 2004 at 23:39 UTC
    There are several ways to bind events to tk widgets. Take a look at Re: Binding Tk events and its parent node.

    davidj

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 12:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found