Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How do I make a static text clickable in wxPerl?

by ZJ.Mike.2009 (Scribe)
on May 27, 2011 at 04:04 UTC ( #906918=perlquestion: print w/replies, xml ) Need Help??

ZJ.Mike.2009 has asked for the wisdom of the Perl Monks concerning the following question:

For example, in the following code:
use strict; use warnings; use Wx; package MyApp; use base 'Wx::App'; sub OnInit { my $frame = Wx::Frame->new( undef, -1, 'A Clickable Label', ); my $label = Wx::StaticText->new( $frame, -1, "Click here to exit", ); $frame->Show; } MyApp->new->MainLoop;
How can I make the static text respond to a mouse event like a button does so that I can click $lable to exit? Thanks for any help.

Replies are listed 'Best First'.
Re: How do I make a static text clickable in wxPerl?
by Anonymous Monk on May 27, 2011 at 05:57 UTC
    How can I make the static text respond to a mouse event like a button does so that I can click $lable to exit?

    Written like someone who skipped reading the event handling overview. Use EVT_MOUSE_EVENTS or create a wxEvtHandler subclas...

    use strict; use warnings; use Wx; package MyApp; use base 'Wx::App'; sub OnInit { my $frame = Wx::Frame->new( undef, -1, 'A Clickable Label', ); my $label = Wx::StaticText->new( $frame, -1, "Click here to exit", + ); my $sizer = Wx::BoxSizer->new( Wx::wxVERTICAL() ); $frame->SetSizer($sizer); $sizer->Add($label); $sizer->Add( Wx::Button->new( $frame, -1, "Click here to exit $_", + ) ) for 1 .. 3; Wx::Event::EVT_MOUSE_EVENTS( $_[0], sub { #~ perl -MWx -le "print for sort keys %Wx::MouseEvent:: " my ( $s, $e ) = @_; #~ LeftIsDown MiddleIsDown RightIsDown my $IsDown = join( '|', grep { my $m = $_ . 'IsDown'; eval { $e->$m; } } qw' Left Middle Right ' ); $IsDown = sprintf 'IsDown< %s >', $IsDown if length $IsDow +n; #~ AltDown ButtonDown CmdDown ControlDown LeftDown MetaDown MiddleDown + RightDown ShiftDown my $Down = join( '|', grep { my $m = $_ . 'Down'; $e->$m; } qw' Alt Button Cmd Control Left Meta Middle R +ight Shift ' ); $Down = sprintf 'Down< %s >', $Down if length $Down; #~ ButtonDClick LeftDClick MiddleDClick RightDClick my $DClick = join( '|', grep { my $m = $_ . 'DClick'; $e->$m; } qw' Button Left Middle Right ' ); $DClick = sprintf 'DClick< %s >', $DClick if length $DClic +k; #~ IsButton IsPageScroll my $Is = join( '|', grep { my $m = 'Is' . $_; $e->$m; } qw' Button PageSc +roll ' ); $Is = sprintf 'Is< %s >', $Is if length $Is; my $Bool = join( '|', grep { my $m = $_; $e->$m; } qw' Button Dragging Entering Leaving Moving ' ); $Bool = sprintf '< %s >', $Bool if length $Bool; #~ GetButton GetLinesPerAction GetLogicalPosition GetPosition GetPosit +ionXY GetWheelDelta GetWheelRotation GetX GetY my $Get = sprintf "Get< %s >", join( '|', map { my $m = 'Get' . $_; sprintf '%s( %s )', $_, $e-> +$m; } qw' Button LinesPerAction ', #~ LogicalPosition #~ Usage: Wx::MouseEvent::GetLogicalPosition(THIS, dc) #~ Position PositionXY #~ X Y qw' WheelDelta WheelRotation ' ); my $GetEvent = $e->GetEventType; $GetEvent = TypeIntToStr($GetEvent) . "($GetEvent) " . $e->GetEventO +bject; my $XY = sprintf "%4d,%4d ", $e->GetPositionXY; print $XY, join ' ', grep length, $Is, $IsDown, $DClick, $ +Down, $Bool, $Get, $GetEvent, "\n"; $e->Skip; return; } ); $frame->Show; } ## end sub OnInit MyApp->new->MainLoop; BEGIN { my %str2con = map { my $dummy; my $con = Wx::constant( $_, 0, $dummy ); $_ => $con; } grep /^wxEVT_/, keys %Wx::; my %con2str = reverse %str2con; sub TypeIntToStr { $con2str{ +shift } ||'' } } ## end BEGIN __END__
      Thank you Anonymous Monk! The example is very educational. I didn't know it would be this easy to detect mouse events in Wx.
Re: How do I make a static text clickable in wxPerl?
by stefbv (Curate) on May 27, 2011 at 06:19 UTC

    Wx::StaticText 'isa' Wx::Window, as a consequence will respond to window events.

    use strict; use warnings; use Wx; package MyApp; use base 'Wx::App'; use Wx qw{:everything}; use Wx::Event qw(EVT_LEFT_UP); sub OnInit { my $frame = Wx::Frame->new( undef, -1, 'A Clickable Label', ); my $label = Wx::StaticText->new( $frame, -1, "Click here to exit", ); my $label2 = Wx::StaticText->new( $frame, -1, "Click here to not exit :)", ); my $bs = Wx::BoxSizer->new(wxVERTICAL); $bs->Add( $label, 0, wxALL, 5 ); $bs->Add( $label2, 0, wxALL, 5 ); $frame->SetSizer($bs); EVT_LEFT_UP $label, sub { $frame->Close }; $frame->Show; } MyApp->new->MainLoop;
    This works for this example, but I can't say for sure if it is the right thing to do.

    Regards, Stefan

      Thank you Stefan. This works like charm :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://906918]
Approved by ww
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2023-01-29 18:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?