PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 105: How to set the button to the exact size you want - Part 1?
Written by kksou   
Sunday, 03 December 2006
Problem

There are some stuff that are supposed to be simple and fundamental, but for some reason, are not that "straightforward" in php-gtk. Setting the size of a GtkButton is one example.

Suppose you want your button to be of size 80x32, if you just use:

$button = new GtkButton('button');
$button->set_size_request(80, 32);
$vbox->pack_start($button);

You will find the button expanding to fill up the entire window, even though you have used set_size_request:

Even if you set the expand and fill to false:

$button = new GtkButton('button');
$button->set_size_request(80, 32);
$vbox->pack_start($button, 0, 0);

You will get this:

Let's stuff the button into a hbox:

$button = new GtkButton('button');
$button->set_size_request(80, 32);
$hbox = new GtkHBox();
$hbox->pack_start($button, 0, 0);
$vbox->pack_start($hbox);

You will get this:

 

So what should you do if you really want the button to be exactly of size 80x32 as shown below:

How to set the button to the exact size you want - Part 1?


Solution
  • Stuff the button in a GtkHBox. Don't forget to set the expand and fill to false when packing the button into the hbox.
  • When stuffing the above hbox in the container, don't forget to set the expand and fill to false too.

Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 150);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Set the size of button to exactly 80 x 32");
$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);

$button = new GtkButton('button'); // note 1
$button->set_size_request(80, 32); // note 1
$hbox = new GtkHBox(); // note 2
  • 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. Create the button and set its size.
  2. Create a HBox and stuff the button into it. Don't forget to set the expand and fill to false.
  3. Stuff the HBox in the containing VBox. Set the expand and fill to false too.

Related Links

User reviews   Average user ratings:    5.0   (from 2 users)
  1. kalikiana
    August 29, 2007 11:07pm

    This article is not useful. You should not use fixed sizes with gtk because it messes up different user configurations. You can't know if the font and theme of the user will render a particular size like you intended. Instead you should pack widgets in such a way that size doesn't matter at all.

  2. Rodrigues
    July 22, 2008 4:07pm

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

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved