Problem
You would like to set up a spin button for the user to enter a numeric value between a given range as shown below:
Solution
- Create a new spin button with GtkSpinButton::new_with_range (double min, double max, double step).
- Note that a GtkSpinButton is a descedent of GtkEntry. To prevent the user from entering values other than the values within the range, we set the entry field to be non-editable with GtkEditable::set_editable(
false
)
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 | <?php $window = new GtkWindow(); $window->set_size_request(400, 120); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox()); // display title $title = new GtkLabel("Set up GtkSpinButton"); $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.5, 0, 0); $alignment->add($title); $vbox->pack_start($alignment, 0); $vbox->pack_start(new GtkLabel(), 0); $vbox->pack_start($hbox = new GtkHBox(), 0); $hbox->pack_start(new GtkLabel('Please select a value: '), 0); $spin_button = GtkSpinButton::new_with_range(10, 20, 2); // note 1 $spin_button->set_editable(0); // note 2 $hbox->pack_start($spin_button, 0); $window->show_all(); Gtk::main(); ?> |
Output
As shown above.
Explanation
- Create the spin button. Here we set it to be in the range 10 to 20 with an increment value of 2.
- Set the entry field to be non-editable.
Read more...