Using the timeout call for animations



Hi Jens,

I'm CC:ing this to the gtk-perl list as perhaps others are interested
as well.

The answer is that you cannot take the drawing out of the exposure
event loop. Event driven programming is different from sequential
programming in that you can never keep the control for too long
a time. You have to give it up, as others may request it as well.
The way to solve this in the gtk environment is by using the
Gtk->timeout_add() call to change the picture. In this call you
can then make a call to the exposure callback which will redraw
the whole widget.

Here is an example of how to use the timeout_add call:

Regards,
Dov

#!/data/alg/local/gcx/bin/perl
######################################################################
#  Example of how to use timers to do animations.
######################################################################

use Gtk;

my @labels = qw(There is more than one way to do it **PERL**);

sub create_widgets {
    my $window = Gtk::Widget->new("Gtk::Window",
                                  -type    => "-toplevel",
                                  -title   =>   "GtkHTTP",
                                  -delete_event => sub { exit }
                                 );
    
    my $label = Gtk::Widget->new("Gtk::Label",
                                 -width=>100,
                                );
    $window->add($label);
    
    $window->show_all();
    Gtk->timeout_add(500, \&set_label, $label );
}

{
    my $counter = 0;
    
    sub set_label {
        my($label) = shift;
        
        $label->set( $labels[$counter++ % @labels] );

        return 0 if $counter == @labels;  # stops animation
        return 1; 
    }
}

Gtk->init;

create_widgets();

main Gtk;

On Mon Nov 05, 2001 at 12:00:54PM +0100, Jens Luedicke wrote:
Hi ...

Btw, did you find some use for my Gtk::WindowDraw module?

Not yet, but I would like to know if it is possible to move
the drawing stuff out of window_event. I want to do the drawing
while the Gtk main loop runs, so I can do little animations.

Currently it seeems that I do the drawing in window_event
and then they are static. I can't make changes.

-- 
Jens Luedicke
jens irs-net com



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