Re: progress bar



On Thu, 2002-06-13 at 22:42, bpmedley 4321 tv wrote:
Hello again,

I'm having a couple more difficulties with gtkperl, this time with the
progress bar.  I've googled around and searched the archives, but don't
know why my code isn't working.  All I want todo is to create a progress
bar and update it to 100% in increments of 10%.  I created timeout_add
function todo this, but on the 2nd iteration I get a "Use of uninitialized
value in subroutine entry" error.

If you have time please run the attached perl code and let me know how it
works for you.  Any help is appreaciated...

-- 
~'`^`'~=-.,__,.-=~'`^`'~=-.,__,.-=~'`^`'~=-., \|/  (___)  \|/ _,.-=~'`^`
                          Brian Medley         @~./'O o`\.~@
"Knowledge is Power" brian medley verizon net /__( \___/ )__\  *PPPFFBT!*
  -- Francis Bacon                               `\__`U_/'
 _,.-=~'`^`'~=-.,__,.-=~'`^`'~=-.,__,.-=~'`^`'~= <____|'  ^^`'~=-.,__,.-=
~`'^`'~=-.,__,.-=~'`^`'~=-.,__,.-=~'`^`'~=-.,__,.-==--^'~=-.,__,.-=~'`^`

----


#!/usr/bin/perl -w

use Gtk;

set_locale Gtk;
init Gtk;

$percent = 0.0;

$window = new Gtk::Window('toplevel');
$progress = new Gtk::ProgressBar(); 
$window->signal_connect( "destroy", sub { Gtk->exit(0); } );
$window->add($progress);
$window->show_all;

Gtk->timeout_add(500, \&UpdateProgress);

main Gtk;

sub UpdateProgress
{
    $percent += 0.1;

    if (1.0 == $percent) {
        Gtk->exit(0);
    }
    
    $progress->update($percent);
}

Following is my solution (note: your example has been edited to my
style).  There were two problems with your code.  The first was what you
noticed.  The function called by timeout events must return true if it
is to continue to exist.  The second was the fact that you should never
assume that a float will hold exactly the number you put in (ie never
say == on a float, you should either test a range with a small delta or
clip it like I did).  Also, always use the strict pragma.

<example>
#!/usr/bin/perl -w

use strict;

use Gtk;

Gtk->set_locale;
Gtk->init;

my $window = Gtk::Window->new('toplevel');
my $pbar   = Gtk::ProgressBar->new(); 
$window->signal_connect( "destroy", sub { Gtk->exit(0); } );
$window->add($pbar);
$window->show_all;

Gtk->timeout_add(500, \&pbar_increment, $pbar, 0.1);

Gtk->main;

sub pbar_increment {
        my ($pbar, $inc) = @_;

        my $new_value = $pbar->percentage + $inc;
        $new_value = 1.0 if $new_value > 1.0;

        print "$new_value\n";

        $pbar->update($new_value);

        return $new_value < 1.0;
}
</example>


-- 
Today is Setting Orange the 19th day of Confusion in the YOLD 3168
Frink!

Missile Address: 33:48:3.521N  84:23:34.786W




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