Need help with Wx and loops

Discussion in 'Perl Misc' started by Steve, Feb 24, 2010.

  1. Steve

    Steve Guest

    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, Feb 24, 2010
    #1
    1. Advertising

  2. Steve

    Steve Guest

    On Feb 24, 9:45 am, Steve <> 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.
    Steve, Feb 24, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob

    help with infinite loops and scanf

    Rob, Jul 26, 2003, in forum: C Programming
    Replies:
    8
    Views:
    768
    Peter Shaggy Haywood
    Jul 29, 2003
  2. Fei Liu

    Need help, loops...

    Fei Liu, Apr 22, 2010, in forum: C++
    Replies:
    3
    Views:
    287
  3. Bob Sanders

    Ruby loops and definitions help?

    Bob Sanders, May 15, 2009, in forum: Ruby
    Replies:
    1
    Views:
    97
    Pascal J. Bourguignon
    May 15, 2009
  4. Me
    Replies:
    2
    Views:
    239
  5. _TT_
    Replies:
    3
    Views:
    92
Loading...

Share This Page