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

Hi Monks, I am building a graphical interface for my program, using radio Buttons

I would like to have 3 buttons linked to a same variable, 2 of them have a fixed -value field. I want the last one to be configurable by the user

I would like to configure this value using a ttk_entry, here's an extract of my code

$Mode="Mode1"; my $entry26 = $lf21->new_ttk__radiobutton(-text => "Mode1", -variable +=> \$Mode, -value => "Mode1"); my $entry27 = $lf21->new_ttk__radiobutton(-text => "Mode2", -variable +=> \$Mode, -value => "Mode2"); my $entry28 = $lf21->new_ttk__radiobutton(-text => "Other:", -variable + => \$Mode, -value => $OtherMode ); my $entry29 = $lf21->new_ttk__entry( -validate => 'focusout', -textvariable => \$OtherMode, );

From the tests i did, i can say that $OtherMode takes the value of the entry but, if I select the last button, the $Mode variable contains an empty string

do you have any idea of what i did wrong?

thanks in advance, have a great day :)

Replies are listed 'Best First'.
Re: [Tkx] passing a variable to -value field in radio button
by choroba (Cardinal) on Nov 21, 2017 at 17:34 UTC
    Please, fix the title of your post: replace Tk with Tkx.

    It seems you think the value of the button is tied to the value of the variable, i.e. when the variable changes, the value of the button changes, too. But that's not the case, it's similar to

    my $mode = 'old'; my $entry_mode = $mode; $entry_mode = 'new'; print $mode; # Still 'old'.

    You need to configure the button to change its value every time the entry changes.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      thanks for the answer, that confirms my thoughts

      I don't think i can change the title as I posted my message as anonymous :/

      the fix i used is to use -command => sub{$Mode = $OtherMode} but now the problem is the button is shown as unchecked if i click on it. Is there another way to do so? I can't find any example similar to what i wan on the internet :(

        What do you make of this? Its not unlike megawidgets

        #!/usr/bin/perl -- use strict; use warnings; use Tkx; Main( @ARGV ); exit( 0 ); sub Main { $Tkx::TRACE = 64; my $mw = Tkx::widget->new( "." ); my $f = $mw->new_ttk__frame(); my $textvar = RadiosEntry( $f, 'good', 'morning', 'sunshine' ); $f->g_pack; undef $f; $f = $mw->new_ttk__frame(); my $b = $f->new_button( -text => "Selection" ); $b->configure( -command => [ \&OnButtonTextFromTextvar, $b, $textv +ar ] ); $b->g_pack; $f->new_ttk__label( -text, 'yo', -anchor => "center" )->g_pack; $f->new_ttk__label( -textvariable => $textvar, -anchor => "center" + ) ->g_pack; $f->g_pack; Tkx::MainLoop(); } ## end sub Main sub OnButtonTextFromTextvar { warn "OnButtonTextFromTextvar @_"; my( $button, $textvar ) = @_; $button->configure( -text => join " ", 'Selection = ', $$textvar ) +; } sub RadiosEntry { my( $parent, @radios ) = @_; my $textvar = ""; my $selected; for my $radio ( @radios ) { my $button = $parent->new_ttk__radiobutton( -text => $radio, -value => $radio, ); $button->configure( -command => [ \&OnRadioButtonSetTextSetSelected, , $button, \$textvar, \$selected ], ); $button->g_pack; $radio = $button; ## modify @radios } ## end for my $radio ( @radios) my $ent = $parent->new_ttk__entry( -text => "", ); $ent->g_pack; $ent->g_bind( '<Key>', [ \&OnEntryConfigureRadioValueInvokeSelected, $ent, $radios[-1], \$selected ] ); return \$textvar; } ## end sub RadiosEntry sub OnEntryConfigureRadioValueInvokeSelected { warn "OnEntryConfigureRadioValueInvokeSelected @_"; my( $ent, $radio, $selected ) = @_; $radio->configure( -value, $ent->get ); $selected and $$selected->invoke; } sub OnRadioButtonSetTextSetSelected { my( $button, $textvar, $selected ) = @_; $$textvar = $button->configure( '-value' )->[-1]; $$selected = $button; warn "OnRadioButtonSetTextSetSelected @_ $textvar $selected\n"; } __END__

        See also https://www.tcl.tk/man/tcl/TkCmd/ttk_radiobutton.htm