Re: Problems updating a label



Vitor Peres said:
---
#!/usr/bin/perl -w

use strict;
use Gtk2 '-init';
use Glib '-init';

my $window = Gtk2::Window->new();
my $lcall_label = Gtk2::Label->new('Retrieving data...');
$window->add($lcall_label);

Glib::Timeout->add(3000, &update_pcalls($lcall_label));

$window->show_all();
Gtk2->main();

sub update_pcalls {
        my $llabel = shift;
        my $date = time();
        $llabel->set_text($date);
        return 1;
}

what you have is very close to correct, with only a few small changes it
should work. esentailly you're not passing a subroutine reference to the add,
you're passing the return value of it. so it is execuited once to get the
return value and the error comes out (as it should) when the timeout happens
and a function by the name of the return value (1) is called, no such function
exists. you're missing a \ in front of the function reference. also if you
want to pass a paramter to the callback you need to do so through the
user_data paramter rather than directly to the function so:

the line:
  Glib::Timeout->add(3000, &update_pcalls($lcall_label));
should be:
  Glib::Timeout->add(3000, \&update_pcalls, $lcall_label);

not that it matters but you do not need have the '-init' on Glib and you
likely don't need to use Glib explicitly at all. those should have no effect
on the script though.

have a look at man perlref (i think) for the gory details on references under
perl. as a general suggestion any time you're having a problem with something
you might want to grep the examples, gtk-demo, and/or t/ (tests) directory for
uses of the function involved to see how it's being used there. i may save you
some time, trouble, and headaches.

-rm



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