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

I am an experienced programmer learning perl by writing a sudoku program and it has gone very well. I am upgrading the cell edit routine (one of a 9x9 matrix) to make it cleaner and am having some trouble. Each cell is square and has 2 lines of up to 5 chars each currently using Tk::text.

I am having trouble filtering out not-allowed chars such as [a..zA..Z] and punctuation chars. I would like a "-validation" command callback like tk::entry has, but I need 2 lines of text. I have read up on bind and bindkey, but they tend to get too kludgy itemizing all the chars to filter out. I need to allow only [1..9], up, down, left, and right and block all else.

I need advice on which approach to use:

1. use the text widget with object/instance binding to bind all the unwanted chars?.

2. text widget subclassing, with text->InsertKeypress() method overridden with some filtering?

3.somehow make the entry widget work in pairs to simulate 2 lines with the first "flowing" into the second? (I have not researched this yet)

4.Some other widget or approach that I don't know about?

If you have any suggestions or words of wisdom, I would be grateful. Thanks -Eugene

Replies are listed 'Best First'.
Re: Filtering/validating Tk::text input
by zentara (Cardinal) on Dec 06, 2006 at 13:28 UTC
    Option 2 is your best bet. This dosn't seem too klunky.
    #!/usr/bin/perl use warnings; use strict; use Tk; package Tk::MyText; require Tk::Text; use base qw/Tk::Text/; Construct Tk::Widget 'MyText'; sub ClassInit { # print "ClassInit [@_]\n"; my ( $class, $mw ) = @_; $class->SUPER::ClassInit( $mw ); return $class; } sub InsertKeypress { my ( $w, $char ) = @_; # print "@_\n"; if ( $char !~ /\d/ ) { return; } else { shift->SUPER::InsertKeypress( @_ ); } } 1; ############################################ package main; my $top = tkinit; my $text = $top->MyText->pack; MainLoop;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

      zentara,

      It works and is clean. I can block or allow any keys that I need. I found the idea from one of your previous posts from a year or two ago and was leaning in that direction, but needed a nudge. Thanks. -Eugene