For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > March 2007 > HList....









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 HList....
nagandla@gmail.com

2007-03-27, 4:09 am

Hi all...

I am creating two text widgets and i am pushing the values of text
widgets into hlist.......after that i am doing saving the contents to
text files and again placing the text file to hlist etc...
but wat i need is while entering values from text widget i need not
enter duplicate values how can i do that....
here it has to check for both number and name ......if both are same
then only it has to restrict......

here is sample code

use Tk;
use Tk::HList;
use Cwd;
use Tie::IxHash;

my $mw = MainWindow->new;

my $f1 = $mw->Frame()->pack(-side => 'left',-expand => 1,-fill =>
'both',-anchor => 'nw');
my $f2 = $mw->Frame()->pack(-side => 'left',-expand => 1,-fill =>
'both',-anchor => 'nw');

$h = $f1->Scrolled("HList",-columns => 2,-header => 1,-bg => 'white',-
width=>50,-height=>20,-scrollbars=>'w')->pack(-expand => 1,-fill =>
'both');

$h->headerCreate(0,-text=> "Name");
$h->headerCreate(1,-text=> "Num");
$h->columnWidth(0,200);
$h->columnWidth(1,100);


$l1 = $f2->Label(-text => 'Name')->grid(-row =>0, -column =>0 );
$l2 = $f2->Label(-text => 'Num')->grid(-row => 1,-column => 0);

$e1 = $f2->Entry(-textvariable => \$ent1,-width => 20,-bg => 'white')-
>grid(-row => 0,-column => 1);


$e2 = $f2->Entry(-textvariable => \$ent2,-width => 20,-bg => 'white')-
>grid(-row => 1,-column => 1);


$b1 = $f2->Button(-text => "Add",-command => \&add)->grid(-row => 3,-
column => 1);

$b2 = $f2->Button(-text => "Save",-command => \&save)->grid(-row => 4,-
column => 1);

$b3 = $f2->Button(-text => "Open",-command => \&open)->grid(-row => 5,-
column => 1);

my $count = 1;
my %hash = ();
my %hash_add = ();

tie %hash,"Tie::IxHash";
tie %hash_add,"Tie::IxHash";

MainLoop;

sub add {

my $uid = `uuidgen`;
#%hash = ();


$hash_add{$ent1} = $ent2;
foreach my $k(keys %hash_add)
{
print "$k\t $hash_add{$k}\n";
}
$h->add($uid);
$h->itemCreate($uid,0,-text=> $ent1);
$h->itemCreate($uid,1,-text=> $ent2);

$ent1 = undef;
$ent2 = undef;

}



sub open {

$h->delete('all');

$file = $mw->getOpenFile(-defaultextension => ".txt",
-filetypes =>
[['Text', '.txt' ],
],
-initialdir => Cwd::cwd(),
);


print "$file\n";

open(FH,"<$file") or die "$! File Open Error in $file";

#%hash = ();

while(<FH> ) {

my @a = split(" ",$_);

$hash{$a[0]} = $a[1];
}
close(FH);

foreach(sort keys %hash) {

my $uid = `uuidgen`;

$h->add($uid);
$h->itemCreate($uid,0,-text=> $_);
$h->itemCreate($uid,1,-text=> $hash{$_});
$uid = undef;
}


}



sub save {

$h->delete('all');

my $sfile = $mw->getSaveFile(-defaultextension => ".txt",
-filetypes =>
[['Text file', '.txt' ],
],
-initialdir => Cwd::cwd(),
);

open(OUT,">$sfile");

foreach(sort keys %hash_add) {
print OUT $_." ";
print OUT $hash_add{$_}."\n";

print "$_ -> $hash_add{$_}\n";
}

%hash_add = ();

close(OUT);

}

Sponsored Links







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

Copyright 2008 codecomments.com