gnope php gtk gui gnope.org  

Retrieve active radio button

 
Post new topic   Reply to topic    gnope.org Forum Index -> PHP GTK2 Beginners
View previous topic :: View next topic  
Author Message
edlynn



Joined: 29 Sep 2006
Posts: 10

PostPosted: Wed Oct 04, 2006 6:40 pm    Post subject: Retrieve active radio button Reply with quote

I have a 4 radio buttons set in a group called card_type_button. Can anyone please tell me how i can easily retrieve which of the buttons is active?
Back to top
kksou



Joined: 06 Sep 2006
Posts: 25

PostPosted: Thu Oct 05, 2006 9:25 am    Post subject: Reply with quote

Here's a sample code:

Code:

<?php
$window = new GtkWindow();
$window->set_size_request(400, 200);

$window->connect_simple('destroy', array('Gtk','main_quit'));

$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel();
$title->set_markup('<span color="blue" font_desc="Times New Roman Italic 12">
    Button Group Demo</span>');
$vbox->pack_start($title);

// setup grouped radio buttons
$radio1 = setup_radio(null, 'radio button 1', '101'); // note 1
$radio2 = setup_radio($radio1, 'radio button 2', '102'); // note 2
$radio3 = setup_radio($radio1, 'radio button 3', '103');

// pack them inside vbox
$vbox->pack_start($radio1, 0, 0);
$vbox->pack_start($radio2, 0, 0);
$vbox->pack_start($radio3, 0, 0);

// add a status area
$vbox->pack_start($status_area = new GtkLabel('Click a Button'));

// function to simplify the display of grouped radio buttons
function setup_radio($radio_button_grp, $button_label, $button_value) { // note 3
    $radio = new GtkRadioButton($radio_button_grp, $button_label);
    $radio->connect('toggled', "on_toggle", $button_value); // note 4
    return $radio;
}

// call-back function when user pressed a radio button
function on_toggle($radio, $value) { // note 5
    global $status_area;
    $label = $radio->child->get_label();  // note 6
    $active = $radio->get_active(); // note 7
    if ($active) $status_area->set_text("radio button pressed: $label (value = $value)\n"); // note 8
}

$window->show_all();
Gtk::main();
?>


Sample output:



more explanation here:

http://www.kksou.com/php-gtk2/articles/display-and-process-grouped-radio-buttons.php

---
Back to top
edlynn



Joined: 29 Sep 2006
Posts: 10

PostPosted: Thu Oct 05, 2006 11:03 am    Post subject: Reply with quote

So is what we are saying that in order to determine the active button we must set it at the time the radio button is clicked. We cant just access it at any point after?
Back to top
cweiske



Joined: 08 Dec 2005
Posts: 454
Location: Leipzig/Germany

PostPosted: Sun Oct 08, 2006 10:11 am    Post subject: Reply with quote

edlynn, yes.
Back to top
lcd



Joined: 20 Nov 2006
Posts: 1

PostPosted: Mon Nov 20, 2006 3:16 pm    Post subject: Reply with quote

Thank you for the example with the radio buttons in a group.
I have used the example, but have incountered a problem.

When the script starts, the first radio button is selected. But as it has not been clicked, the $value var doesn't contain anything.

If the first radio buttion is the one the user wants to have clicked, the user then have to click on a different radion button, and then click on number one.

I my script the radio buttons are created by filling in data from an array. There might be just one, which then can't be selected, because you can't select something thats already selected.

Is there anyway, not to have any radio buttons selected before the user clicks on one.

lcd
Back to top
kksou



Joined: 06 Sep 2006
Posts: 25

PostPosted: Wed Nov 22, 2006 8:15 am    Post subject: Reply with quote

Add a $radio0 button but don't display it.

Sample output:



Sample code:

<?php
$window = new GtkWindow();
$window->set_size_request(400, 200);

$window->connect_simple('destroy', array('Gtk','main_quit'));

$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel();
$title->set_markup('<span color="blue" font_desc="Times New Roman Italic 12">
Button Group Demo</span>');
$vbox->pack_start($title);

// setup grouped radio buttons
$radio0 = setup_radio(null, 'radio button 0', '100'); // note 1
$radio1 = setup_radio($radio0, 'radio button 1', '101');
$radio2 = setup_radio($radio0, 'radio button 2', '102');
$radio3 = setup_radio($radio0, 'radio button 3', '103');

// pack them inside vbox
$vbox->pack_start($radio1, 0, 0);
$vbox->pack_start($radio2, 0, 0);
$vbox->pack_start($radio3, 0, 0);

// add a status area
$vbox->pack_start($status_area = new GtkLabel('Click a Button'));

// function to simplify the display of grouped radio buttons
function setup_radio($radio_button_grp, $button_label, $button_value) {
$radio = new GtkRadioButton($radio_button_grp, $button_label);
$radio->connect('toggled', "on_toggle", $button_value);
return $radio;
}

// call-back function when user pressed a radio button
function on_toggle($radio, $value) { // note 5
global $status_area;
$label = $radio->child->get_label(); // note 6
$active = $radio->get_active(); // note 7
print "bp101. $label: $active\n";
if ($active) $status_area->set_text("radio button pressed: $label (value = $value)\n");
#if ($active) $radio->set_active(false);
}

$window->show_all();
Gtk::main();
?>

Regards,
/kksou
Back to top
PepsiCrazy



Joined: 19 May 2008
Posts: 1

PostPosted: Mon May 19, 2008 3:58 pm    Post subject: Reply with quote

Hi there,

I have tested your example with radio buttons, with the instructions kksou had left last and it worked great for me. Smile
Back to top
Display posts from previous:   
Post new topic   Reply to topic    gnope.org Forum Index -> PHP GTK2 Beginners All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Templates enhanced by DigiWiki. Hosting by Tradebit.