For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > February 2006 > [newbie]Tk::Button









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author [newbie]Tk::Button
Carsten Kress

2006-02-14, 9:56 pm

Hi,

I want to build two button which shut call the same function but depending
on a "parameter".
In the book Mastering Perl/Tk.I found somthing like this:

# Use the same sub for many Buttons
$b = $mw->Button(-text => 'Red', -command => [\&change_color, 'red'])->pack;
$b = $mw->Button(-text => 'Blue',
-command => [\&change_color, 'blue'])->pack;
$b = $mw->Button(-text => 'Green',
-command => [\&change_color, 'green'])->pack;but how do I evaluate the
values (red,blue,green) in the calling function change_color?$_ seems not
workingThanks to the audienceCarsten


Marc Dashevsky

2006-02-14, 9:56 pm

Carsten Kress <kressy@gmx.net> writes in article %:
> Hi,
>
> I want to build two button which shut call the same function but depending
> on a "parameter".
> In the book Mastering Perl/Tk.I found somthing like this:
>
> # Use the same sub for many Buttons
> $b = $mw->Button(-text => 'Red', -command => [\&change_color, 'red'])->pack;
> $b = $mw->Button(-text => 'Blue',
> -command => [\&change_color, 'blue'])->pack;
> $b = $mw->Button(-text => 'Green',
> -command => [\&change_color, 'green'])->pack;but how do I evaluate the
> values (red,blue,green) in the calling function change_color?$_ seems not
> workingThanks to the audienceCarsten



use strict;
use Tk;

my $mw = tkinit;
foreach (qw/Red Blue Green Yellow/) {
$mw->Button(
-text => $_,
-bg => $_,
-command => [\&Change, $mw, $_],
)->pack;
}
MainLoop;

sub Change {
my($w, $color) = @_;
$w->configure(-bg => $color);
}

--
Go to http://MarcDashevsky.com to send me e-mail.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com