in reply to Perhaps it already exists
Here is a nice way using the -listvariable array option. Clicking on an entry will send it to the opposite box. The arrays are nice because you can keep them sorted easily. You can probably combine the update and update1 subs by passing in the src and dst arrays, but this way is less complicated.
#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = tkinit; my @employs = ('A'..'Z'); my @active = (); my $l1 = $mw->Scrolled('Listbox', -listvariable => \@employs, -scrollbars =>'osow')->pack( -side => 'left' ); my $l2 = $mw->Scrolled('Listbox', -listvariable => \@active, -scrollbars =>'osow')->pack( -side => 'left',-padx=>10 ); my $button = $mw->Button(-text=>'Print Active', -command => sub{ print "@active\n"} )->pack(-side=>'left'); $l1->bind( '<ButtonRelease-1>', sub { update( $_[0], $l2 ) } ); $l2->bind( '<ButtonRelease-1>', sub { update1( $_[0], $l1 ) } ); MainLoop; sub update { my ( $src, $dst ) = @_; my $index = $src->curselection; if ($index) { my $value = $src->get($index); # print "$value\n"; @employs = grep { $_ ne $value } @employs; push @active, $value; @employs = sort @employs; @active = sort @active; } } sub update1 { my ( $src, $dst ) = @_; my $index = $src->curselection; if ($index) { my $value = $src->get($index); # print "$value\n"; @active = grep { $_ ne $value } @active; push @employs, $value; @employs = sort @employs; @active = sort @active; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perhaps it already exists
by deesler (Novice) on Dec 24, 2008 at 21:31 UTC |