PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou




3.4 Passing additional data to callback function - Part 1
Written by kksou   
Monday, 12 May 2008

Objective

In HTML, we can use

    [input type="radio" name="radiogrp1" value="NY"]New York

to attach a value to each radio item. When the user select, say New York, the value returned will be NY.

We will achieve the same effect using php-gtk in this example.


Overview

  • Set up the radio buttons as outlined in the previous example.
  • Specify any additional data to be passed along with the signal after the second argument in the connect statement.
  • Note that your callback function definition should now contain the corresponding additional parameters to receive the additional data.

Sample Output

3.4.gif

Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
<?php
$window = new GtkWindow();
$window->connect_simple('destroy',array('Gtk','main_quit'));
$window->set_size_request(200, 100);
$vbox = new GtkVBox();
$window->add($vbox);
$vbox->pack_start(new GtkLabel('Select a State:'), false);

$radio = null;
$radio_button_def = array('New York'=>'NY', 'California'=>'CA', 'Washington'=>'WA');
foreach ($radio_button_def as $state_name => $state_code) {
    $radio = new GtkRadioButton($radio, $state_name);
    $radio->connect('toggled', 'on_toggle', $state_code); // note 1
    $vbox->pack_start($radio, false);
}

$window->show_all();
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.

Explanation

  1. By specifying $state_code as the third argument, the 2-letter state code will also be passed to your callback function by php-gtk.
  2. The first parameter is the widget that emitted the signal. This is passed along automatically by php-gtk. The second parameter $user_data is the additional data that you have specified in the connect statement.

 

Add comment


Security code
Refresh

< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Links - Classes - Social Business - BPM - Web - General
Copyright © 2006-2013. kksou.com. All Rights Reserved