in reply to Re^2: Tk: Dynamic Radiobuttons toggle problem...
in thread Tk: Dynamic Radiobuttons toggle problem...

Radiobuttons that have the same -value and -variable are effectively the same button, even if there are dozens of instances.

Putting Radiobuttons in different Frames does not group them; they are grouped by sharing the same -variable and distinguished within the group by different -values.

The -variable for the Radiobutton should be a reference to a scalar, e.g. \$something, \$hash{key}, \$array[6], etc. When the Radiobutton is selected - by the user or by calling $rdb->select - the -variable assigned to it is set to its -value. The reverse is also true: if the code sets the $something to a value matching a Radiobutton's -value, that Radiobutton becomes selected.

use strict; use warnings; use Tk; use Tk::Radiobutton; my $mw = Tk::MainWindow->new; my $rb_value; # undef, no Radiobutton selected $mw->Radiobutton(-text=>'first',-value=>'one',-variable=>\$rb_value)-> +pack(-anchor=>'w'); $mw->Radiobutton(-text=>'second',-value=>'one',-variable=>\$rb_value)- +>pack(-anchor=>'w'); $mw->Radiobutton(-text=>'third',-value=>'three',-variable=>\$rb_value) +->pack(-anchor=>'w'); $mw->Label(-text=>'------------------------')->pack; my $other_value = '0000ff'; # preset blue $mw->Radiobutton(-text=>'RED',-value=>'ff0000',-variable=>\$other_valu +e)->pack(-anchor=>'w'); $mw->Radiobutton(-text=>'GREEN',-value=>'00ff00',-variable=>\$other_va +lue)->pack(-anchor=>'w'); $mw->Radiobutton(-text=>'BLUE',-value=>'0000ff',-variable=>\$other_val +ue)->pack(-anchor=>'w'); MainLoop; print 'rb:',$rb_value,$/; print 'other:',$other_value,$/;

Replies are listed 'Best First'.
Re^4: Tk: Dynamic Radiobuttons toggle problem...
by iMisspell (Novice) on Feb 27, 2010 at 21:57 UTC
    Thanks so much Keszler, you've been a great help and also to you, stefbv, for posting the example code (added to my own code bank for future posting for others).

    With the code im posting in this post, everything is working correctly and im pretty sure i understand whats going on :)

    By doing the following (@$names[0] = unique file ID)...
    -value=>"both_@$names[0]", -variable=>"both_@$names[0]"
    -value=>"install_@$names[0]", -variable=>"both_@$names[0]"
    The 'variable' is grouping the two buttons for each row because it is using the files unique ID along with the same prefix.
    Since the 'value' is also using the files unique ID along with the same prefix(s) "defined/available" in the 'variable', it will attach itself to the correct r-buttons and toggle correctly.

    Am i understanding that correctly ?

    Here's the 'sub', revised and cleaned up a little if any one is interested.
    sub setExensionFilesChk{ my $extensionNameIndex = $_[0] || return ; $extensionNameIndex = ($extensionNameIndex -1); my $extNameIs = "$extensionNames[$extensionNameIndex][0]"; $extFileFrame->destroy if $extFileFrame; $extFileFrame = $fileListPane -> Frame(); $extFileFrame->configure( -width=>575, -height=>400 ); $extFileFrame -> grid(-row=>1,-column=>1,-columnspan=>4,-sticky=>" +nw"); my $rowIndex = 1; # start with 1 so that 'row' 0 is empty and resu +rved for the "total count label" my ($rdb1,$rdb2,$but,$lab); my @tmpFiles = getExtensionsFiles("$extNameIs"); for my $names ( @tmpFiles ) { $rowIndex++; # add 'Delete' button... $but = $extFileFrame -> Button(-text=>"x", -command => [ \&set +FileType_BothOrInstall_Or_Delete, "@$names[0]", 'delete', \$extension +NameIndex ]); $but -> grid(-row=>$rowIndex,-column=>0, -sticky=>"w"); # Radio Buttons... $rdb1 = $extFileFrame -> Radiobutton(-text=>"Both", -value=>"b +oth_@$names[0]", -variable=>"both_@$names[0]", -command => [ \&setFil +eType_BothOrInstall_Or_Delete, "@$names[0]", 'both' ]); $rdb1 -> grid(-row=>($rowIndex),-column=>1, -sticky=>"w"); $rdb2 = $extFileFrame -> Radiobutton(-text=>"Install", -value= +>"install_@$names[0]", -variable=>"both_@$names[0]", -command => [ \& +setFileType_BothOrInstall_Or_Delete, "@$names[0]", 'install' ]); $rdb2 -> grid(-row=>($rowIndex),-column=>2, -sticky=>"w"); if("@$names[1]" eq "both"){ $rdb1 -> select(); $rdb2 -> deselect(); } else { $rdb1 -> deselect(); $rdb2 -> select(); } # label with file path and file name... $lab = $extFileFrame -> Label(-text=>" - @$names[2]@$names[3]" +); $lab -> grid(-row=>($rowIndex),-column=>3, -sticky=>"w"); } @tmpFiles=(); my $lbl_extCount = $extFileFrame -> Label(-text=>"($rowIndex) Exte +nsion Files:"); $lbl_extCount -> grid(-row=>1,-column=>3,-columnspan=>1, -sticky=> +"w"); $extensionsFilesWindow->update; }