in reply to How to validate text in Text widge

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

Replies are listed 'Best First'.
Re^2: How to validate text in Text widge
by Kafka (Acolyte) on Jan 03, 2016 at 12:22 UTC

    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;
        Thanks!, Exactly what I needed, and very helpful

      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__