|
Problem You have displayed an array of buttons in a row using GtkHButtonBox in How to display arrays of buttons using GtkButtonBox - Part 1 - with different GtkButtonBoxStyle?
Now you would like to set the spacing between the buttons as shown below:

Solution
- You can use GtkBox::set_spacing() to set the spacing between the buttons.
- However, note that this works only for the layouts
Gtk::BUTTONBOX_START and Gtk::BUTTONBOX_END.
- The method has no effect on the following layout:
Gtk::BUTTONBOX_DEFAULT_STYLE, Gtk::BUTTONBOX_SPREAD, Gtk::BUTTONBOX_EDGE. Spacing in these layouts are automatically calculated.
- Note also that the default spacing for
Gtk::BUTTONBOX_START and Gtk::BUTTONBOX_END is 0, that is, all the buttons will stick together in these layouts. I've included one with default spacing in the fourth row of buttons. Compare this with the fifth row, which is set with a spacing of 10.
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
| <?php $window = new GtkWindow(); $window->set_size_request(480, 240); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Display Arrays of Buttons using GtkButtonBox\n". "Part 2 - set spacing between buttons"); $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); $title->set_justify(Gtk::JUSTIFY_CENTER); $alignment = new GtkAlignment(0.5, 0, 0, 0); $alignment->add($title); $vbox->pack_start($alignment, 0); $vbox->pack_start(new GtkLabel(), 0);
// setup button
$buttons = array('button 1', 'button 2', 'button 3', 'button 4'); $buttonbox= setup_hbuttonbox($vbox, $buttons); $buttonbox= setup_hbuttonbox($vbox, $buttons, Gtk::BUTTONBOX_SPREAD, 10); $buttonbox= setup_hbuttonbox($vbox, $buttons, Gtk::BUTTONBOX_EDGE, 10); $buttonbox= setup_hbuttonbox($vbox, $buttons, Gtk::BUTTONBOX_START); $buttonbox= setup_hbuttonbox($vbox, $buttons, Gtk::BUTTONBOX_START, 10); $buttonbox= setup_hbuttonbox($vbox, $buttons, Gtk::BUTTONBOX_END, 10);
$window->show_all(); Gtk::main();
function setup_hbuttonbox($container, $buttons, $layout=Gtk::BUTTONBOX_DEFAULT_STYLE, $spacing=0) { $buttonbox = new GtkHButtonBox(); $buttonbox->set_layout($layout); // note 1
foreach($buttons as $button_label) { $button = new GtkButton($button_label);
|
- 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 The above example is based on the code from Part 1.
What's new here:
- Set the layout.
- Set the spacing.
Note
This example shows GtkHButtonBox. It works the same for GtkVButtonBox.
Related Links
|