eventsevent-handlingwxwidgetswxperl

WxWidget/WxPerl, more instances of WxApp, EVENT handling


I'm trying make a script, which create more then one simple window (it inherit from WxApp). Window contains only one button and handler for click EVENT.

Problem is handling click event, when more then one window is exist. If exists only one instance of window, event is detected correctly. It seems, event works out correctly always in last created window. I can't figure out, where could be problem... In this sample, after clicking on button in first created window, it seems, thet event is catched in second window. It prints "2" to console, instead of "1".

package MyApp;
use base 'Wx::App';

use strict;
use warnings;
use Wx;
use aliased 'Widgets::Forms::MyWxFrame';

sub new {

    my $self = shift;
    $self = {};
    $self = Wx::App->new( \&OnInit );
    bless($self);

    $self->{"windowNumber"} = shift;

    my $mainFrm = MyWxFrame->new(
        undef,                     
        -1,                        
        "My app - ".$self->{"windowNumber"},
        &Wx::wxDefaultPosition
    );

    my $button = Wx::Button->new( $mainFrm, -1, "Test btn", );
    Wx::Event::EVT_BUTTON( $button, -1, sub { __OnClick( $self, @_ ) } );

    $mainFrm->Show(1);
    return $self;
}

sub OnInit {
    return 1;
}

sub __OnClick {
    my $self  = shift;
    my $btn   = shift;
    my $event = shift;

    print $self->{"windowNumber"};
}

my $myApp = MyApp->new(1);
my $myApp2 = MyApp->new(2);


$myApp->MainLoop;

Solution

  • You seem to be fatally confused by the difference between wxApp and wxFrame. The first one represents the entire application and there can only be one of them (in a non pathological situation, anyhow). To create multiple top level windows it's enough to create multiple wxFrames, but your code doesn't do it, it really creates multiple wxApp instances instead.

    Don't do this, create a single wxApp and then create as many windows as you need.