Re: notebook tabs in an for loop set get text labels and set get values of a Simplelist




On Tuesday, May 11, 2004, at 05:49 AM, whitewindow 4950 net wrote:

in my programm i make the notebook tabs in an for loop

code :

for ( $x=0;$x<=$searchoften1-1;$x++){

[snip creating a whole bunch of widgets in "my" vars, and packing them into a notebook page]
 }

this is my for loop to generate my notebooktabs after this i would like have the values from the Simplelist (and set) and the labeles (get and set text)
how can i do this ?

so, basically, you want to use the widgets after creating and forgetting about them.

that means you need to have a reference to each widget at some later time, so you can call methods on it.

there are two ways:

1) walk the widget tree to find the widgets. this is A Bad Idea, because your access code is now very easily broken by changes to your layout. Do Not Do This.

2) store references to the widgets in a list of hashes, and use those references later.

e.g.:

my @pages = ();
for my $pageno (1..$searchoften1) {
        # create all the widgets normally, like you are now:
        my $simplelist = new Gtk2::SimpleList (...);
        my $field1 = new Gtk2::Entry;
        my $field2 = new Gtk2::Entry;
        ...
        $box->pack_start ($simplelist, ...);
        $box->pack_start ($field1, ...);
        $box->pack_start ($field2, ...);
        ...
        $notebook->append_page (...);

        # now store references to the ones in which you are interested:
        push @pages, {
                list => $simplelist,
                field1 => $field1,
                field2 => $field2,
                ...
        };
}

then later on, you can use those with ease, and rather readably:

        ...
        # which page?
        my $i = $notebook->get_current_page;
        my $widgets = $pages[$i];
        # do something with the widgets
        my @selected = $widgets->{list}->get_selected_indices;
        my $field1val = $widget->{field1}->get_text;
        my $field2val = $widget->{field2}->get_text;
        ...


note that having the references in this data structure will keep the widgets alive even after they have been destroyed, so you'll have to clean up that data structure yourself (e.g. in the destroy signal handler for the notebook).

--
Jolt is my co-pilot.
  -- Slogan on a giant paper airplane hung in Lobby 7 at MIT.
     http://hacks.mit.edu/




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]