Wednesday, April 13, 2011

listbox example in perl tk

#!/usr/bin/perl
use warnings;
use strict;
use Tk;

my $mw = MainWindow->new;
$mw->title("Listbox");

# For example purposes, we will use one word for each letter
my @choices = qw/alpha beta charlie delta echo foxtrot golf hotel
india juliet kilo lima motel nancy oscar papa quebec radio sierra
tango uniform victor whiskey xray yankee zulu/;

#Create the text box and insert the list of choices in to it
my $lb = $mw->Scrolled("Listbox",
       -scrollbars => "osoe",
       -height => 10,
       -width => 10,
       -selectmode => "multiple");

my $real_lb = $lb->Subwidget('scrolled');
$real_lb->configure(-
borderwidth=>0);
$lb->insert("end", sort @choices);
$lb->pack(-side => "left");


$mw->Button(-text => "Exit",
             -command => sub{exit; })->pack(-side => "bottom");

$mw->Button(-text=>"View",
             -command => sub {
         foreach ($lb->curselection()) {
                        print "$choices[$_]\n";
                }
      my $w = $real_lb->width;
      print "$w\n";
      my $sample = ' ' x 10;
      my $font_len = $lb->fontMeasure('default', $sample );        
      print "$font_len\n";   


     }
    )->pack(-side => "bottom");


$lb->selectionSet('end');
$lb->see('end');
MainLoop;




OR


open(CON, "<", "c:/MyContacts.txt") || open(CON, "<", "MyContacts.txt");
@contact = <CON>;
@contact = grep {chomp; s/\s+\d+//} @contact;
close CON;

$Search_Listbox = $mw->Scrolled("Listbox",
    -background => "white",
    -scrollbars => "osoe",
    -height => 10,
    -width => 15,
    -selectmode => "multiple",
    -background=>"#8BB381",
    -listvariable => \@contact);
$Search_Listbox->place(-x => 350, -y => 10);

No comments: