The second example will be slightly more complicated. First, we will create a class derived from Wx::Frame, second we will create some (well, just a button) controls inside the frame. The button will do not do anything useful yet.
Top level windows include frames and dialogs. While controls are usually placed directly inside a dialog, they should not be placed directly inside a frame. The reason is that, unlike a dialog, a frame does not provide support for control navigation. If you need to place controls on a frame, you should instead create them inside a panel and put this panel inside the frame.
Subclassing a wxPerl window is as easy as with any other Perl class:
package MyFrame; use base 'Wx::Frame';then of course you will want to create a specialized constructor for your new subclass. The following snippet is exactly what you would expect to work:
sub new { my $ref = shift; my $self = $ref->SUPER::new( undef, # parent window -1, # ID -1 means any 'wxPerl rules', # title [-1, -1], # default position [150, 100], # size );Next you need to create the controls to be put inside the frame. To do so you can finally put to use the "parent" argument to new that you left empty until now.
# controls should not be placed directly inside # a frame, use a Wx::Panel instead my $panel = Wx::Panel->new( $self, # parent window -1, # ID ); # create a button my $button = Wx::Button->new( $panel, # parent window -1, # ID 'Click me!', # label [30, 20], # position [-1, -1], # default size );The rest of this example is exactly like the preceding one except that inside OnInit a MyFrame instance is created instead of a Wx::Frame one.
package MyApp; use base 'Wx::App'; sub OnInit { my $frame = MyFrame->new; $frame->Show( 1 ); } package main; my $app = MyApp->new; $app->MainLoop;
And finally here is how this looks on screen (well, not pretty, but I only had TWM installed...).
Here you can see the program at a glance (it is suffixed .pl.txt so your browser will not try to execute it, but from Perl's point of view this makes no difference).