For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > March 2007 > Retrieving the value of an Selected Item from a Listbox widget









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 Retrieving the value of an Selected Item from a Listbox widget
doni

2007-03-14, 7:07 pm

Hi,

I would like to know how can I retrieve the value of the selected item
from the Listbox widget. I tried different options in Listbox widget
(<Double-ButtonPress-1>, <Return> ) but I was not able to retrieve the
expected results.
Can anyone let me know how can I do this.

Thanks,
doni

#!/usr/bin/perl

use strict;
use Tk;

my @received_hello; my @sending_rupd; my @changing_l2; my
@received_nreg; my @received_nreg_new;
my @sending_nreg_ack; my @received_nd; my @received_nd_ack; my
@sending_nd; my @removing_l2;

my $mw = MainWindow->new;
$mw->configure(-title => 'Test Tool', -background => 'white', -width
=> "700", -height => "500");

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

my $btn1 = $mw->Button(-text => "RFROUTED",
-command => \&rfrouted_button)->pack(-side =>
'right', -anchor => 'e');
my $btn2 = $mw->Button(-text => "GWD",
-command => \&gwd_button)->pack(-side =>
'right', -anchor => 'e');
my $btn3 = $mw->Button(-text => "Exit",
-command => sub{exit})->pack(-side => 'right', -
anchor => 'e', -after => $btn1);
my $fr = $mw->Frame(-background => 'cyan')->pack(-side => 'top', -
fill => 'x');
my $t = $mw->Scrolled("Text")->pack(-side => 'bottom', -fill =>
'both', -expand => '1');
MainLoop();

sub rfrouted_button {
repack();
foreach (qw/rcvd_hello sendg_rupd chng_l2 rcvd_nreg rcvd_nreg_new
sendg_nreg_ack rcvd_nd rcvd_nd_ack sendg_nd rmvg_l2/) {
$fr->Button(-text => $_, -command => \&rcvd_hello, -width =>
'12')->pack(-anchor => 'e');
}
}

sub gwd_button {
repack();
foreach (qw/new_device route_update/) {
$fr->Button(-text => $_, -width => '12')->pack(-anchor =>
'w');
no_of_times(@new_device);
}
}

sub repack {
$btn1->packForget();
$btn2->packForget();
$lbl1->packForget();
$lbl2->packForget();
}

sub rcvd_hello {
my @list = ("00:13:50:00:05:19", "00:13:50:00:05:bd",
"00:13:50:00:04:f9", "00:13:50:00:05:cb");
$lbox1->insert('end', @list);
$lbox1->bind('<<ListboxSelect>>',
sub{ display_lb_curselection($lbox1) });
}

sub display_lb_curselection {
my $lbox1 = shift;
my @cs = $lbox1->curselection();
my $selection = $lbox1->selectionSet(@cs);
print "Current choices: ", $selection;
print "\n";
}

doni

2007-03-14, 7:07 pm

I was able to retrieve the value of the item selected from the Listbox
widget using get option.
Can anyone suggest me how can I do multiple selections from Listbox
widget..

On Mar 14, 10:48 am, "doni" <doni.se...@gmail.com> wrote:

> I would like to know how can I retrieve the value of the selected item
> from the Listbox widget. I tried different options in Listbox widget
> (<Double-ButtonPress-1>, <Return> ) but I was not able to retrieve the
> expected results.
> Can anyone let me know how can I do this.
>
> sub rcvd_hello {
> my @list = ("00:13:50:00:05:19", "00:13:50:00:05:bd",
> "00:13:50:00:04:f9", "00:13:50:00:05:cb");
> $lbox1->insert('end', @list);
> $lbox1->bind('<<ListboxSelect>>',
> sub{ display_lb_curselection($lbox1) });
>
> }
>
> sub display_lb_curselection {
> my $lbox1 = shift;
> my @cs = $lbox1->curselection();
> my $selection = $lbox1->selectionSet(@cs);


Here is the change I made to the above line:
my $selection = $lbox1->get(@cs);

Thanks,
doni


attn.steven.kuo@gmail.com

2007-03-14, 7:07 pm

On Mar 14, 11:23 am, "doni" <doni.se...@gmail.com> wrote:
> I was able to retrieve the value of the item selected from the Listbox
> widget using get option.
> Can anyone suggest me how can I do multiple selections from Listbox
> widget..
>


(snipped)


use Tk;


my $mw = MainWindow->new;
my $lbox = $mw->Scrolled(
'Listbox',
-scrollbars => 'osoe',
# -selectmode => 'browse',
-selectmode => 'multiple',
-exportselection => 0,
)->pack(
-side => 'top'
);

my @choices = qw/eeny meeny miny moe/;
$lbox->insert('end', @choices);

$lbox->bind(
'<ButtonRelease-1>',
sub {
my $curselection = $lbox->curselection;
if ($curselection) {
my @slice = @choices[ @$curselection ];
print "You chose: ";
print join ", " => @slice;
print "\n";
} else {
print "Nothing selected?\n";
}
}
);

MainLoop();

--
Hope this helps,
Steven

Sponsored Links







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

Copyright 2008 codecomments.com