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

Hi there,

I have an application which needs to perform some action when a radio button is deselected. This is what happens when the radio button has previously been clicked, then another radio button is clicked.

The Tk::Radiobutton widget has a callback, -command, but this is only called when the button is clicked, i.e. selected. It is not called when a different button sharing the same -variable is clicked

I know it's possible to code a common subroutine for all the radio buttons in the group, but this is outside my control as I am trying to write a new composite widget (which by implication will not know about other radio buttons outside the widget, unless its -variable changes).

Presumably, there is some internal method in the widget that is called in order for the radio button to get visually unclicked. All I am after is access to this event as a callback, in the manner of -deselect_command below:

my $foo; for (qw/eenie meenie minie mo/) { $mw->Radiobutton( -text => $_, -command => sub { print "$_ selected\n" }, # -deselect_command => sub { print "$_ unselected\n" }, -variable => \$foo, -value => $_ ) ->pack( -side => 'left'); }
--
I'm Not Just Another Perl Hacker

Replies are listed 'Best First'.
Re: Need a callback for Tk radio button deselect
by zentara (Cardinal) on Sep 30, 2003 at 16:35 UTC
    Does this give you ideas?
    #!/usr/bin/perl use Tk; use strict; my $val; my $mw = new MainWindow; $mw->Checkbutton(onvalue => "I am selected", offvalue => "I am deselected", variable => \$val) ->pack; $mw->Entry(textvariable => \$val)->pack; MainLoop;
      zentara, I'm not sure your example helps.

      What I am after is a callback for a radiobutton. My radiobutton widget is detecting changes of value of the common -variable, and visibly changes its appearance. It looks like you are doing something equivalent by typing into the entry box. However, no callback results; this is what I am really after, in order to do some processing when the widget is deselected.

      --
      I'm Not Just Another Perl Hacker
Re: Need a callback for Tk radio button deselect
by zentara (Cardinal) on Oct 01, 2003 at 15:53 UTC
    I'm really just a beginner at Tk. Sorry. Maybe you could separate out the Radio Buttons into single units, and do something like the following:
    #!/usr/bin/perl use Tk; my $mw = MainWindow->new; my $lastbutton = 0; my $rb1 = $mw->Radiobutton(-text => "Button One", -value => 'button1', -variable => \$rb, -command => [ \&showRB, \$rb ])->pack; my $rb2 = $mw->Radiobutton(-text => "Button Two", -value => 'button2', -variable => \$rb, -command => [ \&showRB, \$rb ])->pack; my $rb3 = $mw->Radiobutton(-text => "Button Three", -value => 'button3', -variable => \$rb, -command => [ \&showRB, \$rb ])->pack; my $rb2 = $mw->Radiobutton(-text => "Button Four", -value => 'button4', -variable => \$rb, -command => [ \&showRB, \$rb ])->pack; MainLoop; sub showRB { print "Arg list is @_\n"; my $state_ref = shift; my $state = $$state_ref; $mw->messageBox(-title => 'Status', -type => 'OK', -message => "Status is :$state:. Previous State +was $lastbutton"); $lastbutton = $state; }
Re: Need a callback for Tk radio button deselect
by rinceWind (Monsignor) on Oct 03, 2003 at 09:19 UTC
    For those interested in this thread, I posted this question to the ptk list, and I was pointed in the direction of Tk::Trace, which looks like it will do what I want.

    I will be able to set a trace on the radiobutton variable, which results in a callback when its value is changed. I will need to filter the cases for when the button is being deselected.

    $me =~ /happy bunny/