Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Tk::Text Validation

by willyyam (Priest)
on Apr 21, 2005 at 00:51 UTC ( [id://449821]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to capture the contents of a Text field in a Tk window, and then do something only if the field had text in it. It would look like this:

my $data = $text -> get('1.0','end'); if ($text actually_contains_text) { do some stuff } else { skip to the next step }
I have not been able to find a condition that triggers - it seems like there is a single newline harvested from the text field, but testing for it isn't getting me anywhere. Pointers would be appreciated. Thanks.

Replies are listed 'Best First'.
Re: Tk::Text Validation
by graff (Chancellor) on Apr 21, 2005 at 01:58 UTC
    To elaborate a bit on eibwen's reply, the answer depends on what you mean by "actually contains text". Given that you have the full text content (if any) from the widget stored in $data, you just need to choose what conditions are necessary and sufficient -- pick some condition like:
    $data =~ /\S/ # must contain a non-whitespace character $data =~ /\w/ # must contain an alphanumeric character length( $data ) > $minlength # must be at least this long
    I'm sure you can come up with others that are more relevant to your needs. The point is to apply the test to $data, not to the widget.

      I ended up having to go with a length() test, because none of the other tests (!?) are true for an empty Text field from a TK widget. Who knew? Thanks for your help, and that's why I was looking for new ideas - mine weren't working.

Re: Tk::Text Validation
by zentara (Archbishop) on Apr 21, 2005 at 11:30 UTC
    You probably can make use of the "editModified" flag. I didn't do a good job of detecting a paste event, but you might want to work on that.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new( -width => 200, -height => 200 ); my $text = $mw->Text( -height => 10, -width => 40, -wrap => 'word' ); $text->pack( -side => 'top', -fill => 'both' ); $text->bind( '<FocusOut>' => \&callback ); $text->bind( '<KeyPress>' => \&callback ); $text->bind( '<<Paste>>' => \&callback ); #proper way to detect a paste event? MainLoop; sub callback { if ( $text->editModified() ) { print "text has been modified\n"; $text->editModified(0); } }

    I'm not really a human, but I play one on earth. flash japh
Re: Tk::Text Validation
by eibwen (Friar) on Apr 21, 2005 at 00:57 UTC
    I haven't used Tk::Text, but I think you may be looking for something along the lines of the following:

    my $data = $text->get('1.0','end'); # Presumed to return contents of +desired field if ($data =~ /^.+$/) { # non-empty field } else { # empty field }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (9)
As of 2024-04-23 08:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found