Re: Display window immediately



On Tuesday, January 13, 2004, at 08:16 PM, Andrew Cutler wrote:

I'm using Gtk2::TreeStore to display a fairly large amount of grouped
information in drop down lists.

Because of the amount of information being processed in my
create_model() and add_columns() functions it takes 25-30 secs for the
window to appear. How can I make the window appear immediately (so I can
put a progress bar in the bottom)? The guts of the script are below.

you left out the implementations of create_model() and add_columns(), so i can't tell, but i presume you fill the model immediately after creating it, and filling it takes a long time?

then don't do that. create the model and leave it empty; show the window with an empty model, and install a one-shot (idle or timeout) to trigger the model to load.

add_columns should take no time, don't know what your problem is there.

you can update the gui from inside your loop by calling
  Gtk2->main_iteration while Gtk2->events_pending;

or you can figure out how to do your operation in a truly event-driven way (which may not be possible).


or, you could use a splash screen.


here's an example, using a progress bar:

sub add_columns {
        my $treeview = shift;
        $treeview->append_column
                (Gtk2::TreeViewColumn->new_with_attributes
                        ('thing', Gtk2::CellRendererText->new, text => 0));
}

sub create_model {
        my $model = Gtk2::ListStore->new ('Glib::String');
        # defer loading the model until later.
        Glib::Timeout->add (250, sub {
                # now do whatever you have to do to fill the model.
                # we'll simulate real work with a bunch of things that
                # take a while.
                # since we expect it to take a while, pop up a little
                # dialog with a progress bar
                my $dlg = Gtk2::Dialog->new;
                $dlg->set_title ("loading data, please wait...");
                $dlg->set_transient_for ($window);
                $dlg->set_modal (TRUE);
                my $progress = Gtk2::ProgressBar->new;
                $progress->set_fraction (0.0);
                $dlg->vbox->add ($progress);
                $progress->show;
                $dlg->signal_connect (delete_event => sub {TRUE});
                $dlg->show_now;
                # and disable the main window...
                $window->set_sensitive (FALSE);
                foreach (0..100) {
                        $model->set ($model->append, 0, $_);
                        $progress->set_fraction ($_ / 100);
                        $progress->set_text (sprintf '%.1f%%', 100.0*$_/100);
                        # wait ten milliseconds:
                        Glib::Timeout->add (10, sub { Gtk2->main_quit; 0 });
                        Gtk2->main;
                }
                # re-enable the main window.
                $window->set_sensitive (TRUE);
                $dlg->destroy;
                # don't come back.
                FALSE;
        });
        return $model;
}


--
muppet <scott at asofyet dot org>




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