Help: Passing a widget variable to a subroutine 
Author Message
 Help: Passing a widget variable to a subroutine

Hi,

I'm trying to pass a widget variable to a subroutine, but the subroutine
doesn't recognize it as an object. What can I do. What I have is a group
of buttons which all call the same function, but the function acts
depending on which button was clicked. The idea of passing the Button
variable is so that I can change the text of the button to indicate that
it has been activated. Any ideas?

Thanks



Sun, 05 Dec 1999 03:00:00 GMT  
 Help: Passing a widget variable to a subroutine

Quote:

> Hi,

> I'm trying to pass a widget variable to a subroutine, but the subroutine
> doesn't recognize it as an object. What can I do. What I have is a group
> of buttons which all call the same function, but the function acts
> depending on which button was clicked. The idea of passing the Button
> variable is so that I can change the text of the button to indicate that
> it has been activated. Any ideas?

> Thanks

I did something similar to this ages ago when I was creating a list of
buttons and changing text on the button. I believe you want to be
passing a reference to the button variable through to the function,
something like:

$button = $mw->Button( -text => 'OK' )->pack;
# I think you need to do the command configure on a seperate line after
# the button variable has been created. I could be wrong tho but better
# to be safe than sorry when you can't remember!!
$button->configure( -command => [\&change_text, \$button] );

sub change_text {
  my $button_ref = shift;

  $$button_ref->configure( -text => 'Not OK' );

Quote:
}

If you need more assistance I can try and track down what I did exactly,
but I think the above should give you a rough idea. Apologies if the
syntax is not exactly correct but I am doing this from memory

Have fun.

Simon



Mon, 06 Dec 1999 03:00:00 GMT  
 Help: Passing a widget variable to a subroutine

: Hi,

: I'm trying to pass a widget variable to a subroutine, but the subroutine
: doesn't recognize it as an object. What can I do. What I have is a group
: of buttons which all call the same function, but the function acts
: depending on which button was clicked. The idea of passing the Button
: variable is so that I can change the text of the button to indicate that
: it has been activated. Any ideas?

Is this what you're trying to do?

#!/usr/local/bin/perl5

use Tk;

$m=new MainWindow;

$b1=$m->Button(-text => 'do');
$b1->configure(-command => [\&dobut, $b1, 'doughed']);
$b1->pack;

$b2=$m->Button(-text => 'ra');
$b2->configure(-command => [\&dobut, $b2, 'rayed']);
$b2->pack;

$b3=$m->Button(-text => 'mi');
$b3->configure(-command => [\&dobut, $b3, 'mead']);
$b3->pack;

MainLoop;

sub dobut
{

  $win->configure(-state => disabled,-text => $newtext);

Quote:
}

: Thanks

Sure.. next time you might want to post a particular code fragment
to have people help you with you particular problem.

Andrew



Mon, 06 Dec 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Passing Widget Name to Subroutine

2. regex subroutine \) passing variables

3. Question: passing variables to subroutines

4. Passing variables between subroutines

5. Passing variable coordinate to canvas widget?

6. passing hash from a subroutine to other subroutine

7. Passing subroutine as an argument to another subroutine.

8. Q: passing array/hash from one subroutine to another subroutine

9. Passing one subroutine with parameters into another subroutine.

10. Perl5 passing subroutine refs to subroutines

11. Passing a subroutine as a parameter to another subroutine

12. newbie help with anonymous subroutine syntax for text widget key binding

 

 
Powered by phpBB® Forum Software