Gtk2 radio buttons problem

S

Sylvie

Hi group,

I try to use radio buttons in my Gtk2 perl script so that when the user
clicks one a variable changes of value and a sub routine is called. This is
my code:

my $button1 = Gtk2::RadioButton->new(undef,"Radio Button 1");
$button1->signal_connect("toggled" => sub { $value = "button1";
sighandler(); },"Radio Button 1" );
$vbox->pack_start($button1, TRUE, TRUE, 0);
$button1->set_active(TRUE);
$button1->show;

my @group = $button1->get_group;
my $button2 = Gtk2::RadioButton->new_with_label(@group,"Radio Button 1");
$button2->signal_connect("toggled" => sub {$value = "button2";
sighandler(); },"Radio Button 1" );
$vbox->pack_start($button2, TRUE, TRUE, 0);
$button2->show;

sub sighandler {
print "DEBUG: value = $value\n";
}

The problem I'm having is that everytime a new radio button is toggled, the
sighandler gets called twice: once for the old value of the radio button and
one for the new value. So if I start the script and click on Radio Button 2
it prints:

DEBUG: value = Radio Button 1
DEBUG: value = Radio Button 2

If I now change back to Radio Button 1 I see:

DEBUG: value = Radio Button 2
DEBUG: value = Radio Button 1

I tried naming the buttons the same ($button1 -> $button and $button2 ->
$button) but that didn't matter. What am I doing wrong and how can I fix
this?

Thanks in advance, Sylvie.
 
Z

zentara

Hi group,

I try to use radio buttons in my Gtk2 perl script so that when the user
clicks one a variable changes of value and a sub routine is called. This is
my code:
The problem I'm having is that everytime a new radio button is toggled, the
sighandler gets called twice: once for the old value of the radio button and
one for the new value. So if I start the script and click on Radio Button 2
it prints:

You can setup signal blockers. I did it manually here, but if you had
alot of buttons, you could do it all by putting the buttons in a hash,
and looping through the hash to set the blockers/unblockers:

#!/usr/bin/perl
use warnings;
use strict;
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

my $window = Gtk2::Window->new('toplevel');
$window->set_title('Z');
$window ->signal_connect( 'destroy' => \&delete_event );
$window->set_border_width(10);
$window->set_size_request(300,200);

my $vbox = Gtk2::VBox->new( FALSE, 6 );
$window->add($vbox);
$vbox->set_border_width(2);

my $hbox= Gtk2::HBox->new( FALSE, 6 );
$vbox->pack_end($hbox,FALSE,FALSE,0);
$hbox->set_border_width(2);

$vbox->pack_end (Gtk2::HSeparator->new, FALSE, FALSE, 0);

my $button = Gtk2::Button->new_from_stock('gtk-quit');
$hbox->pack_end( $button, FALSE, FALSE, 0 );
$button->signal_connect( clicked => \&delete_event );

my $value;
my $button1 = Gtk2::RadioButton->new(undef,"Radio Button 1");
my $but1_sigid = $button1->signal_connect(
"toggled" => sub { $value = "button1";
sighandler();
},
"Radio Button 1" );

$button1->signal_handler_block ($but1_sigid);

$vbox->pack_start($button1, TRUE, TRUE, 0);
$button1->set_active(TRUE);
$button1->show;

my @group = $button1->get_group;
my $button2 = Gtk2::RadioButton->new_with_label(@group,"Radio Button
2");
my $but2_sigid = $button2->signal_connect(
"toggled" => sub {$value = "button2";
sighandler();
},
"Radio Button 2" );

$button2->signal_handler_block ($but2_sigid);

$button1->signal_connect(
"enter" => sub { $button1->signal_handler_unblock ($but1_sigid) });
$button1->signal_connect(
"leave" => sub { $button1->signal_handler_block ($but1_sigid) });
$button2->signal_connect(
"enter" => sub { $button2->signal_handler_unblock ($but2_sigid) });
$button2->signal_connect(
"leave" => sub { $button2->signal_handler_block ($but2_sigid)});


$vbox->pack_start($button2, TRUE, TRUE, 0);
$button2->show;

$window->show_all();
Gtk2->main;
#####################################
sub delete_event {
Gtk2->main_quit;
return FALSE;
}
########################################
sub sighandler {
print "value-> $value\n";
}

######################################
__END__
 
S

Sylvie

zentara said:
You can setup signal blockers. I did it manually here, but if you had
alot of buttons, you could do it all by putting the buttons in a hash,
and looping through the hash to set the blockers/unblockers:

<snip code>

Thank you, this works almost perfect. Only problem left: when I click on a
radio button and drag the mouse to another radio button (while mouse is
still clicked) the signals get blocked and cannot be unblocked. Do you have
any idea how to fix this?

Thank you so much, Sylvie.
 
Z

zentara

<snip code>

Thank you, this works almost perfect. Only problem left: when I click on a
radio button and drag the mouse to another radio button (while mouse is
still clicked) the signals get blocked and cannot be unblocked. Do you have
any idea how to fix this?

Yeah, teach your users how to use a mouse. :)

I don't see any problem with the operation, as you describe it
on my system(linux). If I enter a radiobutton zone, and
left-click-and-hold-down, then drag to the other button,
nothing happens. Its as if the button says "maybe you changed
your mind, and I won't toggle until I see a button release". If
I let go of the button, the function goes back to normal.

The radiobutton's are set to toggle on a button-release.
So I have no idea why you would want to change that behavior,
or what you are trying to accomplish.

Are you using a recent version of gtk2-perl? Or are you on MSWindows?
If it's a MSWindows problem, maybe you should ask on the
gtk2-perl maillist.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top