Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How to avoid the duplicate selection in perl tk

by vr786 (Sexton)
on Jan 20, 2011 at 05:21 UTC ( [id://883248]=perlquestion: print w/replies, xml ) Need Help??

vr786 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks , i have written a script to select the items in the list-box (by double clicking on the item) ,simultaneously displaying selected items in another list. Here problem is how can i avoid the multiple selections. eg:In list-box iam "test1" get selected twice or more , in other-side it should not display the twice it should display only one item at a time. Can any one help me out of this problem please

#!/usr/binperl -w use strict; use warnings; use Tk; use Tk::LabFrame; use Tk::ROText; my $mw = MainWindow->new(); $mw->geometry("500x400"); my $f1 = $mw->LabFrame( -label => "List-box-selection", -font => 'ukai', -labelside => "acrosstop", )->pack(); my @list = (qw/test1 test2 test3 test4 test5/); my $lb = $f1->Scrolled( "Listbox", -scrollbars => "e", -selectmode => "extended", -font => 'ukai', -activestyle => "dotbox", )->pack(); $lb->insert( 'end', @list ); my $txt_scroll; $lb->bind( '<Double-1>' => sub { my $current_ip = $_[0]->get( $_[0]->curselection ); $txt_scroll->insert( 'end', "SELECTED IP : $current_ip\n" ); }, ); my $f2 = $mw->LabFrame( -label => "Commands Info", -font => 'ukai', -labelside => "acrosstop", )->pack(); $txt_scroll = $f2->Scrolled( 'ROText', -scrollbars => 'osoe', -height => 6, -width => 25, -font => 'ukai', )->pack(); $mw->update(); MainLoop;

Replies are listed 'Best First'.
Re: How to avoid the duplicate selection in perl tk
by Anonymous Monk on Jan 20, 2011 at 05:34 UTC
    • check to see if it is already inserted
    • if it is already inserted, don't insert it again
    • if it is not already inserted, insert it
Re: How to avoid the duplicate selection in perl tk
by johngg (Canon) on Jan 20, 2011 at 10:10 UTC

    To flesh out AnonyMonk's reply, maintain a hash of items inserted and add a little logic to your double-click call-back.

    ... my %inserted = map { $_ => 0 } @list; ... $txt_scroll->insert( 'end', "SELECTED IP : $current_ip\n" ) unless $inserted{ $current_ip } ++; ...

    I hope this is helpful.

    Cheers,

    JohnGG

      if( not $txt_scroll->search(qw' -forwards -exact ', $current_ip, '1.0' +) ){ $txt_scroll->insert( 'end', "SELECTED IP : $current_ip\n" ); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://883248]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 17:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found