For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > March 2007 > Button Value (Button Widget) as an Array in a Subroutine









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 Button Value (Button Widget) as an Array in a Subroutine
doni

2007-03-14, 10:05 pm

Hi,

I would like to know how can I use the retrieved button value from a
button widget as an array variable in a subroutine if the button value
is passed as a paramter to the subroutine.

When a user selects a button from the button widget, I retrieve the
selected button value and pass it to a subroutine as a parameter. I
have declared buttons to have the same name as their array
counterparts. where was wondering how can I use the button value in
the subroutine as an array so that I can list the array values in a
listbox widget.

Thanks,
doni

Here is the code extract....

#!/usr/bin/perl

use strict;
use Tk;

### Variables ###
### Need Clarification: Do we need so many global arrays. Is there
anyway can we avoid using so many arrays ###
my %gw_route = ();
my %rf_route = ();
my @route_update; my @new_device;
my @received_hello; my @sending_rupd; my @changing_l2; my
@received_nreg; my @received_nreg_new; ##### Array Declarations
my @sending_nreg_ack; my @received_nd; my @received_nd_ack; my
@sending_nd; my @removing_l2;
my @hello_date;
my $key; my $i;

..........................
..........................
##### Create a MainWindow #####
my $mw = MainWindow->new;
$mw->configure(-title => 'Gateway Log Analyzer Tool', -background =>
'cyan', -width => "700", -height=> "500");

################################
### Create necessary Widgets ###

##### Create Listbox #####
my $lbox1 = $mw->Scrolled('Listbox', -scrollbars => 'osoe');
$lbox1->configure(-height => 8, -width => 20);
$lbox1->configure(-selectmode => 'browse');
$lbox1->pack(-anchor => 'e');

##### Listbox for displaying results #####
my $lbox2 = $mw->Scrolled('Listbox', -scrollbars => 'osoe');
$lbox2->configure(-height => 10, -width => 40);
$lbox2->configure(-selectmode => 'browse');
$lbox2->pack(-anchor => 'e');

##### Create buttons, labels and frames for MainWindow #####
my $btn1 = $mw->Button(-text => "RFROUTED",
-command => \&rfrouted_button)->pack(-anchor
=> 'e');
my $btn2 = $mw->Button(-text => "GATEWAYD",
-command => \&gatewayd_button)->pack(-anchor
=> 'e');
my $lbl1 = $mw->Label (-text => "")->pack();
my $btn3 = $mw->Button(-text => "Exit",
-command => sub{exit})->pack(-anchor => 'e', -
after => $btn1);
#my $fra1 = $mw->Frame(-background => 'cyan')->pack(-side => 'top', -
fill => 'x');
#my $t = $mw->Scrolled("Text", -background => 'white')->pack(-side
=> 'bottom', -fill => 'both', -expand => '1');

MainLoop;

### Subroutine to take care of RFROUTED button from the MainWindow ###
sub rfrouted_button {
repack();
foreach (qw/received_hello sending_rupd changing_l2 received_nreg
received_nreg_new ##### Declaring Buttons
sending_nreg_ack received_nd received_nd_ack
sending_nd removing_l2/) {
### Right now stuck up in how can I modify the selected button
to be the array
### Easy way is having an if loop that checks to match the $_
variable
my $btn4 = $mw->Button(-text => $_, -command => sub
{selection_item($_)}, -width => '15')->pack(-anchor => 'e');
}
}

### Subroutine to handle GATEWAYD button from the MainWindow ###
sub gatewayd_button {
repack();
foreach (qw/new_device route_update/) {
$mw->Button(-text => $_, -width => '12')->pack(-anchor =>
'e');
}
}

### Subroutine to repack the widgets once RFROUTED or GATEWAYD is
pressed ###
sub repack {
$btn1->packForget();
$btn2->packForget();
$lbl1->packForget();
}

sub selection_item {
my $selection = @_;
$lbox1->insert('end',
@received_hello);
##### Need to pas specific arrays to insert into an listbox
$lbox1->bind('<Double-ButtonPress-1>',
sub{display_lbox1_curselection($lbox1)})
;
}

sub display_lbox1_curselection {
my $lbox1 = shift;
my @cs = $lbox1->curselection();
$lbox2->delete(0, 'end');
my $selection = $lbox1->get(@cs);
no_of_times($selection, @received_hello);
}

sub no_of_times {
my ($mac_address,@mac_id) = @_;
my %no_of_times;
for $key (@mac_id) {
$no_of_times{$key}++;
}
for $key (keys %no_of_times) {
if ($key eq "$mac_address") {
$lbox2->insert('end', "No of times hello packet has been
sent: ");
$lbox2->insert('end', "$no_of_times{$key} $key");
}
}
}

Ch Lamprecht

2007-03-15, 8:09 am

doni schrieb:
> Hi,
>
> I would like to know how can I use the retrieved button value from a
> button widget as an array variable in a subroutine if the button value
> is passed as a paramter to the subroutine.
>
> When a user selects a button from the button widget, I retrieve the
> selected button value and pass it to a subroutine as a parameter. I
> have declared buttons to have the same name as their array
> counterparts. where was wondering how can I use the button value in
> the subroutine as an array so that I can list the array values in a
> listbox widget.
>


Hi,

instead of using separate variables for the arrays, you might want to use a
hash-based data structure.
my %data = ( received_hello => [],
sending_rupd => [],
changing_l2 => [],
#etc
);
Your button callback will pass a value to your subroutine that matches one of
the %data keys and allows processing of the adressed list.


Christoph

--
use Tk;use Tk::GraphItems;$c=tkinit->Canvas->pack;push@i,Tk::GraphItems->
TextBox(text=>$_,canvas=>$c,x=>$x+=70,y=>100)for(Just=>another=>Perl=>Hacker);
Tk::GraphItems->Connector(source=>$i[$_],target=>$i[$_+1])for(0..2);
$c->repeat(30,sub{$_->move(0,4*cos($d+=3.16))for(@i)});MainLoop
doni

2007-03-15, 10:09 pm

On Mar 15, 2:41 am, Ch Lamprecht <ch.l.n...@online.de> wrote:
> Your button callback will pass a value to your subroutine that matches one of
> the %data keys and allows processing of the adressed list.
>
> Christoph


Thanks for your suggestion, I was able to get the button callback
working in my program. Using Hashes,I was also able to get rid of all
the array usage.

Thanks,
doni

Sponsored Links







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

Copyright 2008 codecomments.com