wxPerl – Getting which button has been clicked

Written by James McDonald

August 14, 2009

I’ve been using wxGlade to create wxPerl interfaces.

This is a sample of how to get one subroutine to handle multiple keys. Clicking any key allows the subroutine to return the label text of which key has been pressed.

wxsamplekeypad

Pressing a key will give a Wx::MessageBox

wxwhichkeypressedmsgbox

I haven’t yet figured out how to return the actual control name (e.g. button_1 , button_2 etc). Sigh.

wxGlade file

Update:
Some information from the wxPerl mailing list you can, instead of leaving the default wxGlade -1 value for the key id and letting wxPerl generate it’s semi random keyid’s, define your own.

That way when you call ->GetId you can test against a predictable value

#define key ID's 
my ($KEY1, $KEY2, $KEY3, $KEY4, $KEY5, $KEY6, $KEY7, $KEY8, $KEY9) = (1, 2, 3, 4, 5, 6, 7, 8, 9);

# update the button creation with the new button ID constants
        $self->{button_1} = Wx::Button->new($self, $KEY1, "1");
	$self->{button_2} = Wx::Button->new($self, $KEY2, "2");
	$self->{button_3} = Wx::Button->new($self, $KEY3, "3");
	$self->{button_4} = Wx::Button->new($self, $KEY4, "4");
	$self->{button_5} = Wx::Button->new($self, $KEY5, "5");
	$self->{button_6} = Wx::Button->new($self, $KEY6, "6");
	$self->{button_7} = Wx::Button->new($self, $KEY7, "7");
	$self->{button_8} = Wx::Button->new($self, $KEY8, "8");
	$self->{button_9} = Wx::Button->new($self, $KEY9, "9");

# you would then put this kind of code in the keyPress event handler sub
if ($KEY1 == $event->GetEventObject()->GetId() ) {
          # do blah
}

 
#!/usr/bin/perl -w -- 
# generated by wxGlade 0.6.3 on Fri Aug 14 14:57:39 2009
# To get wxPerl visit http://wxPerl.sourceforge.net/

use Wx 0.15 qw[:allclasses];
use strict;

package MyFrame;

use Wx qw[:everything];
use base qw(Wx::Frame);
use strict;

sub new {
	my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_;
	$parent = undef              unless defined $parent;
	$id     = -1                 unless defined $id;
	$title  = ""                 unless defined $title;
	$pos    = wxDefaultPosition  unless defined $pos;
	$size   = wxDefaultSize      unless defined $size;
	$name   = ""                 unless defined $name;

# begin wxGlade: MyFrame::new

	$style = wxDEFAULT_FRAME_STYLE 
		unless defined $style;

	$self = $self->SUPER::new( $parent, $id, $title, $pos, $size, $style, $name );
	$self->{button_1} = Wx::Button->new($self, -1, "1");
	$self->{button_2} = Wx::Button->new($self, -1, "2");
	$self->{button_3} = Wx::Button->new($self, -1, "3");
	$self->{button_4} = Wx::Button->new($self, -1, "4");
	$self->{button_5} = Wx::Button->new($self, -1, "5");
	$self->{button_6} = Wx::Button->new($self, -1, "6");
	$self->{button_7} = Wx::Button->new($self, -1, "7");
	$self->{button_8} = Wx::Button->new($self, -1, "8");
	$self->{button_9} = Wx::Button->new($self, -1, "9");

	$self->__set_properties();
	$self->__do_layout();

	Wx::Event::EVT_BUTTON($self, $self->{button_1}->GetId, \&keyPress);
	Wx::Event::EVT_BUTTON($self, $self->{button_2}->GetId, \&keyPress);
	Wx::Event::EVT_BUTTON($self, $self->{button_3}->GetId, \&keyPress);
	Wx::Event::EVT_BUTTON($self, $self->{button_4}->GetId, \&keyPress);
	Wx::Event::EVT_BUTTON($self, $self->{button_5}->GetId, \&keyPress);
	Wx::Event::EVT_BUTTON($self, $self->{button_6}->GetId, \&keyPress);
	Wx::Event::EVT_BUTTON($self, $self->{button_7}->GetId, \&keyPress);
	Wx::Event::EVT_BUTTON($self, $self->{button_8}->GetId, \&keyPress);
	Wx::Event::EVT_BUTTON($self, $self->{button_9}->GetId, \&keyPress);

# end wxGlade
	return $self;

}


sub __set_properties {
	my $self = shift;

# begin wxGlade: MyFrame::__set_properties

	$self->SetTitle("A sample keypad program");

# end wxGlade
}

sub __do_layout {
	my $self = shift;

# begin wxGlade: MyFrame::__do_layout

	$self->{sizer_1} = Wx::BoxSizer->new(wxVERTICAL);
	$self->{grid_sizer_1} = Wx::GridSizer->new(3, 3, 0, 0);
	$self->{grid_sizer_1}->Add($self->{button_1}, 0, 0, 0);
	$self->{grid_sizer_1}->Add($self->{button_2}, 0, 0, 0);
	$self->{grid_sizer_1}->Add($self->{button_3}, 0, 0, 0);
	$self->{grid_sizer_1}->Add($self->{button_4}, 0, 0, 0);
	$self->{grid_sizer_1}->Add($self->{button_5}, 0, 0, 0);
	$self->{grid_sizer_1}->Add($self->{button_6}, 0, 0, 0);
	$self->{grid_sizer_1}->Add($self->{button_7}, 0, 0, 0);
	$self->{grid_sizer_1}->Add($self->{button_8}, 0, 0, 0);
	$self->{grid_sizer_1}->Add($self->{button_9}, 0, 0, 0);
	$self->{sizer_1}->Add($self->{grid_sizer_1}, 1, wxEXPAND, 0);
	$self->SetSizer($self->{sizer_1});
	$self->{sizer_1}->Fit($self);
	$self->Layout();

# end wxGlade
}


sub keyPress {
	my ($self, $event) = @_;
# wxGlade: MyFrame::keyPress 

	warn "Event handler (keyPress) not implemented";
	$event->Skip;

# end wxGlade

        # this is the key to getting which button is clicked
	my $key = $event->GetEventObject()->GetLabel();
	my $keyid = $event->GetEventObject()->GetId();
	
	Wx::MessageBox("You pressed: $key\nKey ID: $keyid", "wxPerl Sample", wxICON_INFORMATION );

	
}

# end of class MyFrame

1;

1;

package main;

unless(caller){
	local *Wx::App::OnInit = sub{1};
	my $app = Wx::App->new();
	Wx::InitAllImageHandlers();

	my $frame_1 = MyFrame->new();

	$app->SetTopWindow($frame_1);
	$frame_1->Show(1);
	$app->MainLoop();
}

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Clear HSTS Settings in CHrome

Open chrome://net-internals/#hsts enter the domain in the query field and click Query to confirm it has HSTS settings...

Ubuntu on Hyper-v

It boils town to installing linux-azure # as root or sudo apt-get update apt-get install linux-azure...