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

Hi dear monks, Is there a way to validate text in a Text widget?
Also is there a simple way to enlarge the height of the window when the text entered reaches the width of the text box? i.e., start as one line and when on the last keystroke reaching the end of it, increase the height by 1. Thanks

Replies are listed 'Best First'.
Re: How to validate text in Text widge
by beech (Parson) on Jan 03, 2016 at 11:16 UTC

    Hi dear monks, Is there a way to validate text in a Text widget?

    yes, bind to an event, see Tk::bind and Tk::Text and Tk::Pod ... if you can be more specific I might be able to be more specific

    Also is there a simple way to enlarge the height of the window when the text entered reaches the width of the text box?

    Simplest way is to use Tk::Scrolled

      Can you please give an example how to disable the entry of ';', i.e. when it is pressed, it will not be entered to the text box

      Can you give an example how Scrolled can be changed dynamically according to amount of text entered?

      Thanks
        #!/usr/bin/perl -- use strict; use warnings; use Tk; my $mw = tkinit; my $tt = $mw->Scrolled('Text')->pack; $tt->bind( '<Key>' => sub { warn $Tk::event->k, ' ', $Tk::event->K; } +, ); $tt->bind( 'Tk::Text' => '<Key-semicolon>' => 'NoOp' ); $tt = $mw->Scrolled('Text')->pack; $tt->bind( 'Tk::Text' => '<Key-semicolon>' => sub { warn 'semicolon re +jected '; return !!0; } ); $mw->WidgetDump; use Tk::WidgetDump; $tt->focus; $mw->MainLoop;

        We're here to help you learn... but your question is tantamount to a "gimmé." Rather than ask others to solve your problem without any evidence of effort on your part (the gimmé), please read the documentation, write some code, and if that fails, debug. Then you'll be well served by asking Monks where the problem lies.


        Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
        1. code
        2. verbatim error and/or warning messages
        3. a coherent explanation of what "doesn't work actually means.
        #!/usr/bin/perl -- use strict; use warnings; use Tk; my $mw = tkinit; my $tt = $mw->Scrolled('anonText', -bg => 'white' ); $tt->pack( qw/-expand 1 -fill both / ); $mw->geometry('50x100'); $mw->WidgetDump; use Tk::WidgetDump; $tt->focus; $mw->MainLoop; BEGIN { package Tk::anonText; use base qw/ Tk::Derived Tk::Text /; Construct Tk::Widget 'anonText'; sub Insert { warn "@_ "; my( $w, $string ) = @_; return unless (defined $string && $string ne ''); return if $string eq ';'; return $w->SUPER::Insert( $string ); } 1; } __END__
Re: How to validate text in Text widge
by hotchiwawa (Scribe) on Jan 03, 2016 at 14:40 UTC
    Finally I worked on my script, I created your validation, because I have to do it too :)

    use strict; use warnings; use Tk; my $mw = new MainWindow(-title => "Pk"); my $qst = $mw -> Label(-text => "Entry validation") -> pack(); # punctuation not allowed my $rsp = $mw -> Entry(-validate => "key", -validatecommand => sub {$_[1] !~ /[;,.:!?]/}, -invalidcommand => sub {$mw->bell}) ->pack(); my $button = $mw ->Button(-text => "Quit", -command => sub { exit }) -> pack(); MainLoop; __END__

    Peace
      This is an Entry validation, I need Text validation :(
        I think you have to add your event (button check for example, lost focus...), to check the text, I didn't find this binding on Text widget. Maybe with "Grab" to get all input.
        The following code will tell you if the text has been found (in console) and will scroll to the text searched.
        You could also highlight this text (with Tags) but I don't have enough time and it's out of scope of my application lol (I don't want to create an editor XD).
        I hope it will help you...
        Peace
        use strict; use warnings; use Tk; use feature qw(say); my $lblText = "Search:"; my $searchText = ""; my $txtContent = "Perl/Tk is probably the most widely known GUI for Pe +rl. It is a great interface used by thousands of people. I am writing + this to teach those with no previous knowledge of any programming la +nguage or GUI interface. You should know some Perl (you don't need to + be an expert, but you need to know some basic/intermediate Perl) bef +ore you read this also. Perl/Tk is a module, so it should be fairly e +asy to get. If you don't know how to install modules"; $txtContent .= reverse $txtContent; $txtContent =~ s/ /\n/g; # ponctuation not allowed my $mw = new MainWindow(-title => "Search demo"); my $lbl = $mw -> Label(-text => $lblText)->pack(); my $search = $mw -> Entry(-textvariable => \$searchText, -validate => "key", -validatecommand => sub {$_[1] !~ /[;,.:!?]/}, + -invalidcommand => sub {$mw->bell})->pack(); my $textarea = $mw -> Frame() ->pack(); my $txt = $textarea -> Text(-width => 40, -height => 10)->pack(); $txt->insert('end', $txtContent); my $btnCheck = $mw ->Button(-text => "Check", -command => sub {&check_text();})->pack(); + MainLoop; sub check_text() { return if (length $searchText == 0); say "Searching: ", $searchText; my $result = $txt->search(-nocase => $searchText, 'end'); if (defined($result)) { say "Found at pos: $result"; $txt->see($result); } else { say "Not found!"; } } __END__
Re: How to validate text in Text widge
by hotchiwawa (Scribe) on Jan 03, 2016 at 13:47 UTC