Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

GUI issue

by harika123 (Initiate)
on Oct 18, 2011 at 12:04 UTC ( #932114=perlquestion: print w/replies, xml ) Need Help??
harika123 has asked for the wisdom of the Perl Monks concerning the following question:

hi all,

I need small help related to radio buttons in perl/TK.

This is my code.

sub setup { chomp; $fr_indx = shift; print "STRING: $_\n"; ($type, $name, .......) = split(/,/,$_); $f_filler = $f[$fr_indx]->Label(-text=>" ")->pack(-side=>'top' +,-pady => 0, -padx => 0); $f_switch{$name} = $f[$fr_indx]->Frame->pack(-side => 'top'); $f_switch_label{$name} = $f_switch{$name}->Label( -text => $name, )->pack(-side=>'top'); # Creation of Radiobuttons $f_filler = $f[$fr_indx]->Label(-text=>" ")->pack(-side=>'top',-pa +dy => 0, -padx => 0); #added $f_switch_radio{$name} = $f_switch{$name}->Radiobutton( -text => 'AUTO ', -value => 'auto' , -highlightbackground => black, #-selectcolor => green, -variable => \$f_switch_val{$name}, -command => [\&f_mode], )->pack(-side=>'right',-pady => '0'); if (index($type,"Toggle")>=0) { $f_switch_radio{$name}->bind("<Button>", [\&toggle_off,$name]); $f_switch_val{$name} = 'off'; } else { $f_switch_radio{$name}->bind("<Button>",[\&f_off,$name]); } $f_filler = $f_switch{$name}->Label(-text => " ") ->pack(-side=>'left',-pady => 0, -padx => 0); $f_switch_radio{$name} = $f_switch{$name}->Radiobutton( -text => 'MAN ', -value => 'man' , -highlightbackground => black, #-selectcolor => green, -variable => \$f_switch_val{$name}, -command => [\&f_mode] )->pack(-side=>'right',-pady => '0') ->select(); }

I am creating two radiobuttons "auto" and "man", for each entry in config file.$type and $name , i am reading from config file. For radiobuttons, variable is given as $f_switch_val{$name} hash.

Callback for both the radiobuttons "auto" and "man" is below:

sub f_mode { my $ref = shift; my $name = shift; #For each key value check the value of radio button value while ( my ($name, $value) = each(%f_switch_val) ) { #If "Manual" button is clicked, then do the following if ($f_switch_val{$name} eq "man"){ # Do this } #If "Auto" button is clicked, then do the following elsif ($f_switch_val{$name} eq "auto"){ #Do this } } }

My problem here is , for each radiobutton click, i am checking the hash "%f_switch_val", hence while loop has to multiple iterations.if there are 20 entries in the hash, for each radio button click, loop has to run for 20 times.

I am using while loop because i dont know the exact key corresponding to that radio button.

Can anyone suggest better way to do this.

Thanks, Harika

Replies are listed 'Best First'.
Re: GUI issue
by zentara (Archbishop) on Oct 18, 2011 at 12:47 UTC
    Your example code didn't run for us to easily modify, but here is an idea for reducing or eliminating the iterations. You can pass extra info to the sub in the callback, which will clearly identify which button was pressed. You can make a hash element unique to each radio button if you wanted, like a unique token, and pass that in to the callback.

    Here is a simple example. For further help, make a small fully running code snippet for us to play with. Having to flesh out incomplete code examples, for testing, usually results in monks moving on to the next question.

    #!/usr/bin/perl use Tk; my $mw = MainWindow->new; my $rb = 'first button'; my $rb1 = $mw->Radiobutton(-text => "Button One", -value => 'button1', -variable => \$rb, -command => [ \&showRB, \$rb, 1, 'man' ])- +>pack; my $rb2 = $mw->Radiobutton(-text => "Button Two", -value => 'button2', -variable => \$rb, -command => [ \&showRB, \$rb, 2, 'auto' ])->pack; MainLoop; sub showRB { #print "Arg list is @_\n"; #print "@_\n"; my ($state_ref, $button_num, $type) = @_; my $state = $$state_ref; print "$state $button_num $type\n"; $mw->messageBox(-title => 'Status', -type => 'OK', -message => "Status is :$state: $type button $bu +tton_num."); }

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

      Thanks zentara for your help....It really worked...Thanks alot

Re: GUI issue
by moritz (Cardinal) on Oct 18, 2011 at 12:24 UTC

    You don't need a loop to do a hash lookup.

    if (exists $f_switch_val{'man'}) { ... }

    Is faster, less code and harder to screw up.

    Update: reading the code twice, it appears you are searching for a value, not for a key. If that's the case, the proper solution is to construct the hash the other way round, so that the common lookup is by key, not by value.

Re: GUI issue
by Anonymous Monk on Oct 18, 2011 at 13:22 UTC

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2018-01-23 06:22 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (240 votes). Check out past polls.