Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
XP is just a number
 
PerlMonks  

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__

Comment on Tk vs Tkx for -validatecommand
Download Code
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 Tk.pm-isms

        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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994459]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-04-19 13:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls