Sample Code 109: How to align GtkEntry fields - Part 2?
Written by kksou   
Wednesday, 06 December 2006
Problem

This is another supposedly-simple-but-not-so-straightforward-in-php-gtk2 example.

Please refer to How to align GtkEntry fields - Part 1?

Suppose now you would like to display the field labels as shown below (i.e. with the labels right justified and packed to the left, and the textentry fields aligned to the left):

How to align GtkEntry fields - Part 2?

 

It is common that people get the following:

GtkEntry fields not the right size:

 

GtkEntry fields not aligned to the left:

 

Field labels not aligned to the left:

 

Solution

We make use of the technique as outlined in How to set the button to the exact size you want - Part 2 - using GtkAlignment?, that is, with the use of GtkAlignment.

We also need to set some of the parameters when using GtkTable::attach().


Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Align GtkEntry Fields - Part 2");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);

$fields = array('Item number', 'Item Description', 'Unit price', 'Quantity');
$field_size = array(120, 200, 80, 80);
$entry = array(); // to store the text entries

$table = new GtkTable();
display_table($table, $fields, $field_size); // display the table
$vbox->pack_start($table);

function display_table($table, $fields, $field_size) {
    global $entry;
    $row = 0;
    foreach ($fields as $field) {
        $label = new GtkLabel("  $field: ");
        $alignment = new GtkAlignment(1, .5, 0, 0); // note 1
        $alignment->add($label);
        $table->attach($alignment, 0, 1, $row, $row+1, 
            Gtk::FILL, Gtk::SHRINK, 0, 0); // note 2

        $entry[$row] = new GtkEntry();
        $alignment = new GtkAlignment(0, .5, 0, 0); // note 3
        $alignment->add($entry[$row]);
        $entry[$row]->set_size_request($field_size[$row], -1); // note 4
        $table->attach($alignment, 1, 2, $row, $row+1); // note 5
        ++$row;
    }
}

// create a submit button
$button = new GtkButton('Submit');
$button->set_size_request(60, 28);
  • 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. Align the field label to the right of column 0.
  2. Pack the field label to the left of the table.
  3. Left align the textentry fields using GtkAlignment.
  4. Set the size of the textentry fields.
  5. Align the textentry fields to the left of column 1 of the table.

Related Links

User reviews   Average user ratings:    0.0   (from 3 users)
  1. csc from england
    April 04, 2007 8:48am

    Hi, The above example is very good. I found a bug in the above coding. The variable $row has been set back to 0 after exiting from the function display_table. The Submit button has picked up $row as 0 and after adding 1 to $row, the submit button has overlapped with the first field Item Number.

    Best Regards,
    csc

  2. kksou
    April 04, 2007 10:32am

    If you noticed in line 44: $row = count($fields), the variable $row was set back to the number of fields, which is 4 in this case...

  3. Augusto from Canada
    June 15, 2007 6:38pm

    Very useful code. But I think one important aspect is to add other bouttons in a bouttonbox for example to perform different tasks if I click on exit or Ok or go first or go last. & collect the information from GtkEntry fields & insert it into a database like mysql with php.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved