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

Tk vs Tkx for -validatecommand

by shortyfw06 (Beadle)
on Sep 19, 2012 at 14:00 UTC ( #994459=perlquestion: print w/replies, xml ) Need Help??
shortyfw06 has asked for the wisdom of the Perl Monks concerning the following question:

For an entry widget in Tk, I used the following validate command to only allow negative and positive integers. This would give an immediate error if a letter or other character was entered. In Tkx, I do not get an error when the entry is invalid. Is there a different way to do this in Tkx?

#! perl use strict; use warnings; use Tk; my $value; my $mw = new MainWindow; my $ent = $mw->Entry(-textvariable => \$value, -validate => 'key', -validatecommand => sub { $_[0] =~ /^(?:|-|\d+|-\ +d+)$/ }, -invalidcommand => \&lam_num_error)->pack(); my $print_button = $mw->Button(-text => "Print", -command => \&printx, -font => "ansi 10 bold")->pack(); my $reset_frm = $mw->Frame(); $reset_frm->pack(-fill => 'both'); my $reset_button = $reset_frm->Button(-text => "Reset", -command => \&do_reset, -font => "ansi 10 bold")->pac +k(); MainLoop; sub printx { print $value unless $value eq '-'; } sub do_reset { $ent->delete(0, 'end'); } sub lam_num_error { $mw->messageBox(-message => "The input must be an integer."); } __END__

Replies are listed 'Best First'.
Re: Tk vs Tkx for -validatecommand
by zentara (Archbishop) on Sep 19, 2012 at 15:18 UTC

      I tried the following which includes the callback. It isn't working properly. Does anyone see the problem? Thanks!

      #! perl use strict; use warnings; use Tkx; my $value; my $mw = Tkx::widget->new("."); my $ent = $mw->new_entry(-textvariable => \$value,-validate => 'key')- +>g_pack(); $ent->configure( -validatecommand => [sub { $_[0] =~ /^(?:|-|\d+|-\d+)$ +/ }, Tkx::Ev('%P'), $ent], -invalidcommand => \&lam_num_error); my $print_button = $mw->new_button(-text => "Print", -command => \&printx, -font => "ansi 10 bold")->g_pack(); my $reset_frm = $mw->new_frame(); $reset_frm->g_pack(-fill => 'both'); my $reset_button = $reset_frm->new_button(-text => "Reset", -command => \&do_reset, -font => "ansi 10 bold")->g_p +ack(); Tkx::MainLoop(); sub printx { print $value unless $value eq '-'; } sub do_reset { $ent->delete(0, 'end'); } sub lam_num_error { Tkx::tk___mmessageBox(-message => "The input must be an integer.") +; } __END__

        :) You're still stuck in your

        If you run your program you'll get a nice error

        Can't call method "configure" without a package or object reference

        g_pack doesn't return an object

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994459]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2018-06-25 18:34 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (128 votes). Check out past polls.