Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I'm trying to learn WxPerl and have what I hope is an easy question. With the following code below I'm trying to launch this app and once it starts I then want it to start updating the progress bar based on my sub routine. It must happen automatically. I know I can call my sub via an event through a button etc, but how can I do this automatically after it starts?

use Wx qw(:everything); ####################################### # # package MyApp; # # ####################################### use strict; use vars qw(@ISA); @ISA = qw(Wx::App); sub OnInit { my($this) = @_; my($frame) = MyFrame->new( undef, -1, "Checking Updates", [-1,-1], + [330, 220], "wxCAPTION|wxCLOSE_BOX|wxMINIMIZE_BOX|wxMAXIMIZE_BOX|wxS +YSTEM_MENU|wxRESIZE_BORDER|wxFRAME_TOOL_WINDOW|wxCLIP_CHILDREN" ); $frame->CenterOnScreen; $frame->Show(1); $this->SetTopWindow($frame); return 1; } ####################################### # # package MyFrame; # # ####################################### use strict; use Wx qw[:everything]; use base qw(Wx::Frame); Wx::InitAllImageHandlers(); sub new { my( $class ) = shift; my( $this ) = $class->SUPER::new( @_); Wx::StaticBitmap->new($this, -1, Wx::Bitmap->new("C:\\Path to my b +g image.png", wxBITMAP_TYPE_ANY), wxDefaultPosition, wxDefaultSize, ) +; $this->{gauge} = Wx::Gauge->new($this, -1, 100, [10, 150], [200, +20], wxGA_HORIZONTAL|wxGA_SMOOTH); $this->{gauge}->SetForegroundColour(Wx::Colour->new(216, 216, 191) +); check_for_updates(); return $this; } sub check_for_updates { my( $this ) = @_; # Testing <---Problem is here for (1..100) { $this->{gauge}->SetValue( $_ ); sleep 1; } } package main; my($app) = MyApp->new(); $app->MainLoop();
Any idea what I'm doing wrong?

Thanks

Replies are listed 'Best First'.
Re: Running a routine in WxPerl?
by pc88mxer (Vicar) on Jun 06, 2008 at 19:22 UTC
    Check out the Wx::Timer class to perform periodic processing within your app.
      looks like what I need...thanks!
Re: Running a routine in WxPerl?
by zentara (Cardinal) on Jun 06, 2008 at 19:31 UTC
    I don't know much about Wx, but in Tk( probably similar in concept), you might want to set it up on a timer. Using sleep in for loops, in GUI apps will interfere with the Mainloop functioning. So, in your new block, instead of calling check_for_updates, do something in Wx notation like shown in Perl script does not do anything when run as Windows Service. Instead of sleep, you rechedule the timer every second, and put it in OnInit. You might not need the OnInit, but you probably need to delay it somehow until the MainLoop is up and running.

    I'm not really a human, but I play one on earth CandyGram for Mongo