Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

optionmenu configure problem in perl tk

by vr786 (Sexton)
on Nov 08, 2010 at 07:13 UTC ( [id://870049]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I want to validate the fields , i am trying like this but i don't know where iam going wrong please , any one can explain....it.

#!/usr/bin/perl -w use strict; use Tk; my $opt = "0"; my $opt1 = "0"; my $mw = MainWindow->new(); $mw-> geometry("400x100"); $mw -> configure(-background => "cyan", -foreground => "lightblue" ); my ($var, $var1); $opt = $mw->Optionmenu( -background => "lightgreen", -options => [[jan=>1], [feb=>2], [mar=>3], [apr=>4]], -variable => \$var, -command => \&sub_opt, )->pack; $opt->addOptions([may=>5],[jun=>6],[jul=>7],[aug=>8]); my $exit_but = $mw->Button(-text=>'Exit', -command=>sub{ print "Var= $var Var1= $var1\n"; $mw->destroy}, -state => 'disabled', )->pack(-side=>"bottom"); $opt1 = $mw->Optionmenu( -background => "lightgreen", -options => [qw(test1 test2 test3)], -variable => \$var1, -state => 'disabled', -command =>\&sub_opt1, )->pack; MainLoop; sub sub_opt { if($var ne " ") { $opt->configure(-state => 'disabled'); $opt1->configure(-state => 'normal'); } else { $opt -> configure(-state => 'disabled'); } } sub sub_opt1 { if ( $var1 ne " ") { $opt1->configure(-state => 'disabled'); $exit_but->configure(-state =>'normal'); } else { $exit_but->configure(-state => 'disabled'); } }

Replies are listed 'Best First'.
Re: optionmenu configure problem in perl tk
by biohisham (Priest) on Nov 08, 2010 at 08:30 UTC
    It doesn't seem that Optionmenu objects have support for the configure option. running your code while the lines $opt->configure(...) and $opt1->configure(...) are commented out yields the Tk widget.

    Check Optionmenu Configuration Options...


    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

      Tk::Optionmenu is derived from Tk::Menubutton and probably has support for configuring most, if not all, of its options.

      The documentation is somewhat brief and possibly ambiguous in terms of making a clear distinction between options of the widget (e.g. $w->configure($option => $val); and options of the menu (e.g. $w->addOptions([@new_options]);).

      Try this example code:

      #!perl use 5.12.0; use warnings; use Tk; use Tk::Optionmenu; my $mw = MainWindow->new(); my $om = $mw->Optionmenu( -options => [qw{Zero One Two Three}], )->pack(); my $state = 0; my @states = qw{normal disabled}; $mw->Button(-text => q{Toggle State}, -command => sub { $state ^= 1; $om->configure(-state => $states[$state]); })->pack(); $mw->Button(-text => q{Exit}, -command => sub { exit })->pack(); MainLoop;

      -- Ken

Re: optionmenu configure problem in perl tk
by zentara (Archbishop) on Nov 08, 2010 at 13:24 UTC
    I want to validate the fields

    The only widget I know of in Tk, that supports validation, is the Tk::Entry. Additionally, your program dosn't run as shown, it crashes because of the way you specify your -command callbacks. When you specify

    -command => \&sub_opt,
    the command is executed immediately when the Optionmenu is being created, and it will fail with an error. You want to wrap the command in or a sub{} like this:
    -command => sub { \&sub_opt },
    When you make callbacks, you set them up to be something that is "called later" not immediately.

    Here's a bit more of an explanation:

    #!/usr/bin/perl #for example the following is wrong my $row = 0; my $column = 0; for (my $i = 9; $i >= 0; $i--) { $button{$i} = $mw->Button(-text => "$i", -width => '3', -height => '1', -command => &numpress($i)) ->grid(-row => $row, -column => $column); $column++; if($column > 2){$column = 0; $row++;} } MainLoop; ####################################################### # Now the problem is here: -command => &numpress($i)) # The thing being assigned to the "-command" attribute needs # to be an anonymous subroutine, a reference to a named subroutine, # or else a reference to an array whose elements are: # named_subroutine_ref, arg1(, arg2 ...) -- in other words, # either of the following would be the right way to do what you want: -command => sub { numpress( $i ) } # or -command => [ \&numpress, $i ] #The way you had it written, your subroutine is actually being called #when the Button is being created, and Perl/Tk is trying to use the #return value of the sub as the value for "-command" -- not good.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found