[Fwd: Gtk and Glib::Timeout]

Y

Y.G.

I am writing a program in perl which invokes a small window. This window
should time out after some time. For the window I use Gtk2. For the
timeout I use Glib::Timeout

The code looks something like this:

my $dialog = new Gtk2::Dialog( );

(creating some buttons and all)

my $timer = Glib::Timeout->add( $interval, \&some_func));

Gtk2->main;


This returns Not a CODE reference at /program_path line line_number

Should I use Glib (and how?) or is there any other function I should use.
 
Z

zentara

I am writing a program in perl which invokes a small window. This window
should time out after some time. For the window I use Gtk2. For the
timeout I use Glib::Timeout

The code looks something like this:

my $dialog = new Gtk2::Dialog( );

(creating some buttons and all)

my $timer = Glib::Timeout->add( $interval, \&some_func));

Gtk2->main;


This returns Not a CODE reference at /program_path line line_number

That seems to say that the sub some_func isn't declared yet.
Should I use Glib (and how?) or is there any other function I should use.

All you need to do is return FALSE from the sub after the timeout
period. Timers keep running as long as they return TRUE from the
code reference.

$id = Glib::Timeout->add ($milliseconds, \&timeout_handler);

sub timeout_handler {
do_cool_stuff ();
return $keep_running_or_not;
}


but since it's a 1 shot timer, it can all be in one line,
for a 10 second timeout

my $id = Glib::Timeout->add (10000, sub{ return 0 } );


are you trying to make the dialog only stay up 10 seconds?
Only moderately tested code follows....................

#!/usr/bin/perl
use warnings;
use strict;
use Glib qw(TRUE FALSE);
use Gtk2 '-init';

my $dialog = Gtk2::Dialog->new ('Confirmation Dialog', undef, 'modal',
'gtk-ok' => 'ok', # response ids may be one of the built-in enums...
'_Cancel' => 2, # or any positive integer.
);

# put an hbox with a label and entry into the dialog's vbox area.
my $hbox = Gtk2::HBox->new (FALSE, 6);
$hbox->pack_start (Gtk2::Label->new ('Continue Real Transfer? '), TRUE,
TRUE, 0);
$hbox->show_all;

$dialog->vbox->pack_start ($hbox, TRUE, TRUE, 0);

# Set which response is the default:
$dialog->set_default_response ('ok');

# connect a signal to the dialog's response signal.
$dialog->signal_connect (response => sub {
# get our params
shift; # we don't need the dialog
my $response = shift; # the clicked button

print "The user clicked: $response\n";

if ($response eq 'ok')
{
Gtk2->main_quit;
}
elsif ($response eq 'delete-event')
{
$response = 2;
}
elsif ($response == 2)
{

}
});

# show the dialog
$dialog->show;

my $id = Glib::Timeout->add (5000, sub{
print "timeded out\n";
$dialog->destroy;
return FALSE;
});

Gtk2->main;
__END__
 
Y

Y.G.

zentara said:
That seems to say that the sub some_func isn't declared yet.


All you need to do is return FALSE from the sub after the timeout
period. Timers keep running as long as they return TRUE from the
code reference.

$id = Glib::Timeout->add ($milliseconds, \&timeout_handler);

sub timeout_handler {
do_cool_stuff ();
return $keep_running_or_not;
}


but since it's a 1 shot timer, it can all be in one line,
for a 10 second timeout

my $id = Glib::Timeout->add (10000, sub{ return 0 } );


are you trying to make the dialog only stay up 10 seconds?
Only moderately tested code follows....................

#!/usr/bin/perl
use warnings;
use strict;
use Glib qw(TRUE FALSE);
use Gtk2 '-init';

my $dialog = Gtk2::Dialog->new ('Confirmation Dialog', undef, 'modal',
'gtk-ok' => 'ok', # response ids may be one of the built-in enums...
'_Cancel' => 2, # or any positive integer.
);

# put an hbox with a label and entry into the dialog's vbox area.
my $hbox = Gtk2::HBox->new (FALSE, 6);
$hbox->pack_start (Gtk2::Label->new ('Continue Real Transfer? '), TRUE,
TRUE, 0);
$hbox->show_all;

$dialog->vbox->pack_start ($hbox, TRUE, TRUE, 0);

# Set which response is the default:
$dialog->set_default_response ('ok');

# connect a signal to the dialog's response signal.
$dialog->signal_connect (response => sub {
# get our params
shift; # we don't need the dialog
my $response = shift; # the clicked button

print "The user clicked: $response\n";

if ($response eq 'ok')
{
Gtk2->main_quit;
}
elsif ($response eq 'delete-event')
{
$response = 2;
}
elsif ($response == 2)
{

}
});

# show the dialog
$dialog->show;

my $id = Glib::Timeout->add (5000, sub{
print "timeded out\n";
$dialog->destroy;
return FALSE;
});

Gtk2->main;
__END__
Seems to work. Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top