Problem
This is in response to Bernard Fouch
Solution
- First set up the pulldown menu using GtkComboBoxEntry as outlined in How to setup and process GtkComboBoxEntry - Part 2 - using data view model?
- Each time you want to change the options, just use GtkListstore::clear() to clear the model, then repopulate the model with GtkListstore::append().
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 43 49 50 51 52 53 54 55 56 57 58 59 60 61 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | <?php $window = new GtkWindow(); $window->connect_simple('destroy', array( 'Gtk', 'main_quit')); $window->set_size_request(400,180); $window->add($vbox = new GtkVBox()); // display title $title = new GtkLabel("Change GtkComboBoxEntry options on the fly"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 60); $vbox->pack_start($title, 0, 0); // add buttons $vbox->pack_start($hbox = new GtkHBox(), 0); $hbox->pack_start(new GtkLabel('Continent: '), 0); $hbox->pack_start($button1 = new GtkButton('US'), 0); $button1->connect('clicked', 'on_button'); $button1->set_size_request(80, -1); $hbox->pack_start($button2 = new GtkButton('Europe'), 0); $button2->connect('clicked', 'on_button'); $button2->set_size_request(80, -1); $vbox->pack_start(new GtkLabel(), 0); // leave a gap // the selection $list['US'] = array('US-1', 'US-2', 'US-3', 'US-4'); $list['Europe'] = array('Europe-1', 'Europe-2', 'Europe-3', 'Europe-4', 'Europe-5', 'Europe-6'); $vbox->pack_start($hbox=new GtkHBox(), 0, 0); $hbox->pack_start(new GtkLabel('Select: '), 0, 0); // Create the model if (defined("GObject::TYPE_STRING")) { $model = new GtkListStore(GObject::TYPE_STRING); } else { $model = new GtkListStore(Gtk::TYPE_STRING); } // Create the comboboxentry $combobox = &GtkComboBoxEntry::new_with_model($model, 0); // populate combobox populate('Europe'); // setup the submit button $hbox->pack_start($combobox, 0, 0); $hbox->pack_start(new GtkLabel(' '), 0, 0); $hbox->pack_start($button = new GtkButton('Submit'), 0, 0); $button->set_size_request(60, 24); $button->connect('clicked', "on_submit", $combobox); function on_submit($button, $combobox) { $model = $combobox->get_model(); $selection = $combobox->get_child()->get_text(); // note 3 echo "You have selected: $selection!\n"; } function on_button($button) { $continent = $button->get_label(); echo "continent: $continent\n"; populate($continent); } function populate($continent) { global $model, $list; $model->clear(); // note 1 foreach($list[$continent] as $choice) { $model->append(array($choice)); // note 2 } } $window->show_all(); Gtk::main(); ?> |
Output
As shown above.Explanation
We make use of the code in
Parse error: syntax error, unexpected end of file in /var/www/kksou.com/public_html/php-gtk2/plugins/content/DirectPHP/DirectPHP.php(56) : eval()'d code on line 1
to set up the GtkComboBoxEntry.
What's new here:
- Clear the model.
- Repopulate the model.
- Get the selection.
Related Links
- How to setup and process GtkComboBoxEntry - Part 2 - using data view model?
- How to setup and process GtkComboBoxEntry - Part 1?
- How to change options of pulldown menu on the fly using GtkComboBox?
- How to setup and process GtkComboBox?
- How to setup pulldown menu with 2 columns - Part 1 - using GtkComboBox?
- How to setup and process GtkComboBoxEntry - Part 1?
Read more...