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

Hi monks, I have written a perl tk script to display the current select value in text-scroll. Here is my problem whenever i make double click on list box the selected value should display and previous value should be removed , i tried to delete the previous selected value but it is not working, can any one give me the solution .

#!/usr/bin/perl -w use strict; use warnings; use Tk; use Tk::LabFrame; use Tk::ROText; my $lb ; my $txt_scroll; 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/); $lb = $f1 -> Scrolled("Listbox", -scrollbars => "e", -selectmode => "extended", -font => 'ukai', -activestyle => "dotbox", )->pack(); $lb->insert('end',@list); $lb -> bind('<Double-1>'=> sub { my $current_ip = $_[0]->get($_[0]->curselection),; $txt_scroll->insert('end',"SELECTED IP : $current_ip\n"); }, ); $f1 -> place ( -x => 100 , -y => 55, -width =>"170", -height => "120") +; my $f2 = $mw -> LabFrame( -label => "Commands Info", -font => 'ukai', -labelside => "acrosstop", )->pack(); my $frame_info = $mw -> Frame(); $txt_scroll = $frame_info->Text(); $txt_scroll = $f2 -> Scrolled( 'ROText', -scrollbars => 'osoe', -height => 6 , -width => 25, -font => 'ukai', )->pack();$mw->update(); $f2 -> place( -x => 100, -y => 200, -width => 180, -height => 100, ); MainLoop;

Replies are listed 'Best First'.
Re: Displaying selected value from Listbox in perl tk
by stefbv (Priest) on Dec 20, 2010 at 07:48 UTC

    Try changing the callback sub to:

    $lb -> bind('<Double-1>'=> sub { my $current_ip = $_[0]->get($_[0]->curselection); $txt_scroll->delete( '1.0', 'end' ); # empty first, than inser +t $txt_scroll->insert('end',"SELECTED IP : $current_ip\n"); }, );

    Regards, Stefan.

      Hi Stefan, thanks for replay , i used the above code it is deleting my previous selected items also(I have one more list box).

        Hi,

        It is supposed to do that, delete the contents of the ROText widget, before inserting a new value. It's not what you want?

        I have one more list box

        Maybe because, you use pack and place in the same time?

        After a little cleanup:

        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->delete( '1.0', 'end' ); $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;

        Regards, Stefan