Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Need help with Wx and loops (http://www.velocityreviews.com/forums/t912490-need-help-with-wx-and-loops.html)

Steve 02-24-2010 05:45 PM

Need help with Wx and loops
 
I'm just doing this for learning, and I can't figure it out. What
this is supposed to do is when I click the start button, it constantly
moves anything from 1 folder to another until I click the stop button.

I have 2 problems with my code: 1. strict doesn't like it, and 2. the
button stays pressed forever and I cannot initiate the stop button.

strict doesn't like it because I can't seem to remember exactly how to
declare a global variable. I attempted state $x = 1; but it didn't
like that. So in my code I show it's changed so strict is not in use
and I just declare it as $x = 1; (I know this is wrong :P).

here is my code:

#!/usr/bin/perl -w
#use strict;
use Wx;

################################################## #########
#
# Define importsvc class that extends Wx::App
#
package importsvc;
use base qw(Wx::App); # Inherit from Wx::App
use Wx::Event qw(EVT_BUTTON);
sub OnInit
# Every application has its own OnInit method that will
# be called when the constructor is called.
{
my $self = shift;
my $frame = Wx::Frame->new( undef, # Parent window
-1, # Window id
'Import Services', # Title
[200,300], # position X, Y
[250, 150] # size X, Y
);
my $panel = Wx::Panel->new($frame,-1); #create a panel in the
parent window $frame
my $start = Wx::Button->new($panel,1,"Start Import",[10,10]);
my $stop = Wx::Button->new($panel,1,"Stop Import",[10,40]);
EVT_BUTTON($start,$start,\&startimport);
EVT_BUTTON($stop,$stop,\&stopimport);
$self->SetTopWindow($frame); # Define the toplevel window
$frame->Show(1); # Show the frame

}
sub startimport {
$x = 1;
while( $x == "1"){
system "mv /home/steve/Desktop/INCOMING/* /home/steve/Desktop/
IMPORTED";
}
}
sub stopimport {
$x = 0;
}
################################################## #########
#
# The main program
#
package main;

mkdir "/home/steve/Desktop/INCOMING";
system "touch /home/steve/Desktop/INCOMING/transmission1.txt";
system "touch /home/steve/Desktop/INCOMING/transmission2.txt";
system "touch /home/steve/Desktop/INCOMING/transmission3.txt";
system "touch /home/steve/Desktop/INCOMING/transmission4.txt";
mkdir "/home/steve/Desktop/IMPORTED";
my $wxobj = importsvc->new(); # New HelloWorld application
$wxobj->MainLoop;

Steve 02-24-2010 06:24 PM

Re: Need help with Wx and loops
 
On Feb 24, 9:45*am, Steve <st...@staticg.com> wrote:
> I'm just doing this for learning, and I can't figure it out. *What
> this is supposed to do is when I click the start button, it constantly
> moves anything from 1 folder to another until I click the stop button.
>
> I have 2 problems with my code: 1. strict doesn't like it, and 2. the
> button stays pressed forever and I cannot initiate the stop button.
>
> strict doesn't like it because I can't seem to remember exactly how to
> declare a global variable. *I attempted state $x = 1; but it didn't
> like that. *So in my code I show it's changed so strict is not in use
> and I just declare it as $x = 1; (I know this is wrong :P).
>
> here is my code:
>
> #!/usr/bin/perl -w
> #use strict;
> use Wx;
>
> ################################################## #########
> #
> # Define importsvc class that extends Wx::App
> #
> package importsvc;
> use base qw(Wx::App); * # Inherit from Wx::App
> use Wx::Event qw(EVT_BUTTON);
> * *sub OnInit
> * *# Every application has its own OnInit method that will
> * *# be called when the constructor is called.
> * *{
> * * * my $self = shift;
> * * * my $frame = Wx::Frame->new( undef, * * * * # Parentwindow
> * * * * * * * * * * * * * * * * * -1, * * * * * *# Window id
> * * * * * * * * * * * * * * * * * 'Import Services', # Title
> * * * * * * * * * * * * * * * * * [200,300], * * * * # position X, Y
> * * * * * * * * * * * * * * * * * [250,150] * * # size X, Y
> * * * * * * * * * * * * * * * * );
> * * *my $panel = Wx::Panel->new($frame,-1); #create a panel in the
> parent window $frame
> * * *my $start = Wx::Button->new($panel,1,"Start Import",[10,10]);
> * * *my $stop = Wx::Button->new($panel,1,"Stop Import",[10,40]);
> * * *EVT_BUTTON($start,$start,\&startimport);
> * * *EVT_BUTTON($stop,$stop,\&stopimport);
> * * *$self->SetTopWindow($frame); * *# Define the toplevel window
> * * *$frame->Show(1); * * * * * * * *# Show the frame
>
> * *}
> * *sub startimport {
> * * * * $x = 1;
> * * * * while( $x == "1"){
> * * * * system "mv /home/steve/Desktop/INCOMING/* /home/steve/Desktop/
> IMPORTED";
> * *}
> * *}
> * *sub stopimport {
> * * * * * *$x = 0;
> * *}
> * *################################################## #########
> * *#
> * *# The main program
> * *#
> * *package main;
>
> * *mkdir "/home/steve/Desktop/INCOMING";
> * *system "touch /home/steve/Desktop/INCOMING/transmission1.txt";
> * *system "touch /home/steve/Desktop/INCOMING/transmission2.txt";
> * *system "touch /home/steve/Desktop/INCOMING/transmission3.txt";
> * *system "touch /home/steve/Desktop/INCOMING/transmission4.txt";
> * *mkdir "/home/steve/Desktop/IMPORTED";
> * *my $wxobj = importsvc->new(); # New HelloWorld application
> * *$wxobj->MainLoop;


I fixed it, I learned about the fork command to spawn a child process.


All times are GMT. The time now is 04:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.