Perl Tk 'bind' problem 
Author Message
 Perl Tk 'bind' problem

I've created a form, and each field needs to get validated. I therefore
use 'bind' on 'FocusOut', which runs a function similar to
'ValidateFieldX'. However, if the field is invalid, I need to be able
to update the status message box (a 'Label' widget). I've used a
'textvariable', but the function doesn't seem to want to update it,
although no errors are returned, the widget remains blank.

Having been fiddling with this for some time now, the only clue I have
is that the label does get updated if I only bind one Entry widget it
works. In fact, the last Entry widget that I bind works.

HELP!!!


possible, as I check that regularly. Otherwise, here will be fine.

=====
WORK : CGU, 431 Godstone Road, Whyteleafe, Surrey, CR3 0YQ.
HOME : Flat 3, 91 Park Lane, Croydon, Surrey CR0 1JJ.

0171 662 0110 (Work), 0181 649 9118 (Home), 07712 672422 (Mobile)

____________________________________________________________
Do You Yahoo!?


==========================================================================
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the



Mon, 11 Mar 2002 03:00:00 GMT  
 Perl Tk 'bind' problem
This is a multi-part message in MIME format.
--------------196C35417A8F3FDBECDDEFED
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Quote:

> I've created a form, and each field needs to get validated. I therefore
> use 'bind' on 'FocusOut', which runs a function similar to
> 'ValidateFieldX'. However, if the field is invalid, I need to be able
> to update the status message box (a 'Label' widget). I've used a
> 'textvariable', but the function doesn't seem to want to update it,
> although no errors are returned, the widget remains blank.

> Having been fiddling with this for some time now, the only clue I have
> is that the label does get updated if I only bind one Entry widget it
> works. In fact, the last Entry widget that I bind works.

> HELP!!!


> possible, as I check that regularly. Otherwise, here will be fine.

> =====
> WORK : CGU, 431 Godstone Road, Whyteleafe, Surrey, CR3 0YQ.
> HOME : Flat 3, 91 Park Lane, Croydon, Surrey CR0 1JJ.

> 0171 662 0110 (Work), 0181 649 9118 (Home), 07712 672422 (Mobile)

> ____________________________________________________________
> Do You Yahoo!?


> ==========================================================================
> This message was posted through the Stanford campus mailing list
> server.  If you wish to unsubscribe from this mailing list, send the


I've successfully used a widget "PatternEntry" to validate input, or more
precisely, stopping the user from entering invalid input. PatternEntry.pm is a
subclass of Entry, the only difference is the -pattern option which lets you
specify a regular expression. PatternEntry does not accept input which doesn't
match the pattern.

Example:

    PatternEntry (.... -pattern => '^\d*\.?\d*$')

This PatternEntry will accept any decimal number. If you enter anything but
decimal numbers, the computer beeps.

Another one:

    PatternEntry (... -pattern => '^.{0,4}$')   # Same as LEntry (-maxwidth =>
4)

The pattern is evaluated after each insert or delete. It is important the the
pattern allows an empty string, or that the default string (initial value of
-textvariable) matches the pattern, otherwise PatternEntry won't let you enter
anything. Also remember the start of string (^) and end of string ($), without
them the pattern above will accept everything.

PatternEntry.pm is attached.

Hans J. Helgesen.

--------------196C35417A8F3FDBECDDEFED
Content-Type: text/plain; charset=us-ascii; name="PatternEntry.pm"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="PatternEntry.pm"

package Tk::PatternEntry;
#
# Adds the option -pattern to Tk::Entry.
#

$VERSION = '0.00';

# Load Entry code
require Tk::Entry;

use locale;


# Add a 'constructor' method
 Tk::Widget->Construct('PatternEntry');

# What we do different when created
sub Populate
{

    # Add the extra 'configure' option
    $w->ConfigSpecs (-pattern => ['PASSIVE']);

Quote:
}

# What we do different for insert.
sub insert
{


Quote:
}

sub delete
{


Quote:
}    

sub validate
{

    # Save old value and cursor position
    my $old_value = $w->get;
    my $old_cursor = $w->index('insert');

    # Execute command the way Entry does it.

    # Validate new content if -pattern is defined.
    if (defined(my $pattern = $w->cget (-pattern))) {
        my $new_value = $w->get;
        unless ($new_value =~ m/$pattern/) {
            $w->SUPER::delete(0,'end');
            $w->SUPER::insert(0, $old_value);
            $w->SUPER::icursor ($old_cursor);
            $w->bell;
        }
    }

Quote:
}

1;

--------------196C35417A8F3FDBECDDEFED--

==========================================================================
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the



Mon, 11 Mar 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. How to bind 'Back Space' event

2. perl -e 'use Tk'

3. O'Reilly's Perl/Tk

4. getservbyname('smtp','tcp') Problem

5. Can't load 'C:/perl/site/lib/auto/Tk/Event/Event.dll' for module Tk::Event:

6. Tk::Pretty problem with menu's

7. Calling Tk methods from 'C'

8. Tk::Scrolled('Text') and TCP/IP

9. Tk::Scrolled('Text') and TCP/IP

10. converting from Tk's repeat to wxPerl's Timer

11. Trouble with Tk::Scrolled('Tree')

12. '#!/usr/local/bin/perl -s' problem

 

 
Powered by phpBB® Forum Software