For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > March 2007 > Error Checking on Entry Widget - Perl/Tk









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 Error Checking on Entry Widget - Perl/Tk
doni

2007-03-08, 7:02 pm

Hi,

I am trying to do some error checking on my Entry Widget in this Perl/
Tk test program. I would like to check if the user has entered some
text under Filename before he starts pressing the "Load" button. Will
"validate command" option under Entry Widget work for this. Can anyone
point me how should I go about this.

Here is the source code.
#!/usr/bin/perl

use Tk;

$mw = MainWindow->new;
$mw->configure(-title=>'Test',
-background=>'blue',
-width=>"700",
-height=>"500");

$info = "Start Loading Filename...";

# Create necessary widgets
$f = $mw->Frame->pack(-side => 'top',
-fill => 'x');
$f->Label(-text => "Filename:")->pack(-side => 'left',
-anchor =>
'center');
$f->Entry(-textvariable => \$filename)->pack(-side => 'left',
-
anchor => 'w',
-fill
=> 'x',
-
expand => 1);
$f->Button(-text => "Exit",
-activebackground => "red",
-underline => 1,
-command => sub {exit;})->pack(-side => 'right');
$f->Button(-text=>"Clear",
-command=> sub {$t->delete("0.0",'end')})->pack(-
side=>'right',

-anchor=>'s');
$f->Button(-text => "Save",
-command => \&save_file)->pack(-side => 'right',
-
anchor => 'e');
$f->Button(-text => "Load",
-command => \&load_file)->pack(-side => 'right',
-
anchor => 'e');
$mw->Label(-textvariable => \$info,
-relief => 'ridge')->pack(-side => 'bottom',
-fill => 'x');
$t = $mw->Scrolled("Text")->pack(-side => 'bottom',
-fill => 'both',
-expand => 1);
MainLoop;

# load_file checks to see what the filename is and loads it if
possible
sub load_file {
$info = "Loading file '$filename'...";
$t->delete("1.0", "end");
if (!open(FH, "/$filename")) {
$t->insert("end", "ERROR: Could not open $filename\n");
return;
}
while (<FH> ) { $t->insert("end", $_); }
close (FH);
$info = "File '$filename' loaded";
}

# save_file saves the file using the filename in the Entry box.
sub save_file {
$info = "Saving '$filename'";
open (FH, ">$filename");
print FH $t->get("1.0", "end");
$info = "Saved.";
}

Thanks,
doni

Marc Dashevsky

2007-03-08, 7:02 pm

In article <1173379376.831993.38370@p10g2000cwp.googlegroups.com>, doni.sekar@gmail.com says...
> Hi,
>
> I am trying to do some error checking on my Entry Widget in this Perl/
> Tk test program. I would like to check if the user has entered some
> text under Filename before he starts pressing the "Load" button. Will
> "validate command" option under Entry Widget work for this. Can anyone
> point me how should I go about this.


Here's a sample. perldoc Tk::Entry has a thorough section on validation.

-validate => 'focusout',
-validatecommand => sub {
my($proposed, $changed, $current, $index, $type) = @_;
return(-f $proposed); # fail if not an existing file
},

--
Go to http://MarcDashevsky.com to send me e-mail.
Ch Lamprecht

2007-03-08, 7:02 pm

doni wrote:
> Hi,
>
> I am trying to do some error checking on my Entry Widget in this Perl/
> Tk test program. I would like to check if the user has entered some
> text under Filename before he starts pressing the "Load" button.


Hi doni,

there are also 'perldoc Tk::getOpenFile' (and getSaveFile) Dialogs.

Christoph


--
use Tk;use Tk::GraphItems;$c=tkinit->Canvas->pack;push@i,Tk::GraphItems->
TextBox(text=>$_,canvas=>$c,x=>$x+=70,y=>100)for(Just=>another=>Perl=>Hacker);
Tk::GraphItems->Connector(source=>$i[$_],target=>$i[$_+1])for(0..2);
$c->repeat(30,sub{$_->move(0,4*cos($d+=3.16))for(@i)});MainLoop
doni

2007-03-08, 7:02 pm

On Mar 8, 12:11 pm, Ch Lamprecht <ch.l.n...@online.de> wrote:
> Hi doni,
>
> there are also 'perldoc Tk::getOpenFile' (and getSaveFile) Dialogs.
>
> Christoph
>


Thanks, Chris. getOpenFile and getSaveFile has made the job easy for
me.

Thanks,
doni

Sponsored Links







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

Copyright 2008 codecomments.com