dabella12 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks i'm trying to printout all the text in a Gtk2::ComboBox. I have searched and can not find how to do this anywhere in perl. in python all i have to do is

model=combobox.get_model() for row in model: print row[0]
but when i try this in perl it doesn't work. Any help/suggestion would be appreciated

Replies are listed 'Best First'.
Re: How to printout a Gtk2::ComboBox
by kikuchiyo (Hermit) on Aug 16, 2019 at 22:49 UTC

    It's slightly more complicated: you have to use the foreach method of the model to iterate on its rows, supply a callback to it, in which you use the Gtk2::TreeIter to get the value out of each row.

    Working example:

    #! /usr/bin/perl use strict; use warnings; use feature qw/say/; use Gtk2 -init; my $window = Gtk2::Window->new ('toplevel'); $window->signal_connect (delete_event => sub { Gtk2->main_quit }); my $vbox = Gtk2::VBox->new(); $window->set_size_request (130, 100); $window->add($vbox); my $combo = Gtk2::ComboBox->new_text(); foreach (qw/foo bar baz/) { $combo->append_text ($_); } $vbox->pack_start($combo, 0, 0, 5); # expand?, fill?, padding my $button = Gtk2::Button->new ('extract'); $button->signal_connect (clicked => \&extract); $vbox->pack_start($button, 1,1,5); $window->show_all; Gtk2->main; sub extract { my $active_text = $combo->get_active_text(); print "active text: $active_text\n"; print "all rows:\n"; my $model = $combo->get_model(); $model->foreach(sub { my ($store, $path, $iter) = @_; my $value = $model->get_value($iter, 0); # assuming there is o +ne column only in the model print $value."\n"; return 0; }); }

    Documentation: http://gtk2-perl.sourceforge.net/doc/pod/Gtk2/ComboBox.html - warning: it's autogenerated and obsolete.

Re: How to printout a Gtk2::ComboBox
by swl (Prior) on Aug 16, 2019 at 22:44 UTC

      And just for completeness, here is an example using iter_next. One could argue that the lack of callbacks make this somewhat easier to read than the foreach example.

      my $iter = $model->get_iter_first(); while ($iter) { my @selected = $labels_model->get( $iter ); print @selected; $iter = $model->iter_next( $iter ); }

      See also the more detailed foreach example by kikuchiyo in 11104587.

Re: How to printout a Gtk2::ComboBox
by dabella12 (Acolyte) on Aug 16, 2019 at 19:18 UTC
    my @model=$combo->get_model; foreach(@model) { print $_[0]; }
    This is what i have tried in perl

    2019-08-27 Athanasius fixed code tags.

      Thanks for this code. (you're just missing a </code> terminating tag, but that's of little consequence in this short example).

      I know almost zero about Gtk2 (and I can't find Gtk2::Combobox - do you mean Gtk2::Ex::ComboBox?) but I can say that $_[0] is likely to be undefined here. foreach just sets $_, not @_. Maybe try printing that, or using Data::Dumper or similar to dump the object, if it is one? Just a thought.

      An SSCCE is always best.

        I have tried it both ways with $_ and $_[0]

      The docs for Gtk2::Ex::ComboBox say you want get_treeview to get the model:
      my @model = $combo->get_treeview; print $_ for @model;
      "Returns the treeview that serves as the model for the ComboBox".
Re: How to printout a Gtk2::ComboBox
by dabella12 (Acolyte) on Aug 19, 2019 at 01:39 UTC

    Thank you Monks that is exactly what i was looking for