tag:blogger.com,1999:blog-89194439622928744592024-03-05T17:43:37.962-08:00wxPerl ExamplesTutorials and Examples for wxPerl - the Perl wrapper for the wxWidgets GUI toolkit.Unknownnoreply@blogger.comBlogger2125tag:blogger.com,1999:blog-8919443962292874459.post-84645616298596478282013-03-07T22:08:00.000-08:002013-03-07T23:09:32.472-08:00Laying Out Controls Using SizersThe first in a series of <a href="http://www.wxperl.it/p/manual.html">wxPerl Manual</a> topics, <a href="http://www.wxperl.it/p/sizers-and-layout.html">Sizers and Layout</a> covers the basics of using sizers to control the layout of your windows in wxPerl.Unknownnoreply@blogger.com10tag:blogger.com,1999:blog-8919443962292874459.post-80005316335343004522013-03-07T14:07:00.001-08:002013-03-07T19:22:25.563-08:00Hello WorldThis is the first in a series of basic wxPerl examples showing how to get started with wxPerl. This example simply opens a window with the title 'Hello World'.<br />
<br />
The examples are meant to provide a basic guide; they try to assume only a no prior knowledge of wxWidgets API; for a detailed API description you need to refer to the <a href="http://www.wxperl.it/p/documentation.html">wxWidgets manuals</a>. The key thing you need to keep in mind while browsing wxWidgets documentation is that, if a wxPerl class is called, say, Wx::Frame, you should search for wxFrame in wxWidgets documentation.<br />
<br />
Every Perl script should start by using the strict and warnings pragmas. Here we load the Wx module too.<br />
<br />
<pre style="white-space: pre-wrap; word-wrap: break-word;"><i><span style="color: #6aa84f;">#!/usr/bin/perl</span></i>
use strict;
use warnings;
use Wx;
</pre>
<div>
<br /></div>
Every wxPerl program must create an application object: it manages global state and processes events.<br />
<br />
<pre style="white-space: pre-wrap; word-wrap: break-word;">package MyApp;
use base 'Wx::App';</pre>
<pre style="white-space: pre-wrap; word-wrap: break-word;"></pre>
When an application object is created, wxPerl calls the OnInit method which is used to initialize global application data and usually creates one or more frames.<br />
<br />
<pre style="white-space: pre-wrap; word-wrap: break-word;"><i><span style="color: #6aa84f;"># The OnInit method is called automatically when an
# application object is first constructed.
# Application level initialization can be done here.</span></i>
sub OnInit {
my( $self ) = @_;
<i><span style="color: #6aa84f;"># create a new frame (a frame is a top level window)</span></i>
my $frame = Wx::Frame->new(
undef, <i style="background-color: white;"><span style="color: #6aa84f;"># parent window</span></i>
-1, <i><span style="color: #6aa84f;"># ID -1 means any</span></i>
'Hello World', <span style="color: #6aa84f;"><i># title</i></span>
[-1, -1], <span style="color: #6aa84f;"><i># default position</i></span>
[250, 150], <span style="color: #6aa84f;"><i># size</i></span>
);
<span style="color: #6aa84f;"><i># show the frame</i></span>
$frame->Show( 1 );
<span style="color: #6aa84f;"><i># The OnInit sub must return a true value or the wxApp
# will not start. Although an explicit return is not
# necessary as the $frame->Show line will return
# a true value, we'll include an explicit line
# in this example.</i></span>
return 1;
}</pre>
<br />
Note that wxWidgets requires every window to have an ID which is an integer number assigned at window creation time. In wxPerl most of the time you don't care about the value of the ID, so you should pass -1 or the constant wxID_ANY to the window constructor to let wxWidgets generate an unique ID for you.<br />
<br />
Top level windows are not automatically shown by wxWidgets, hence you need to do it yourself as in the example with $frame->Show(1);<br />
<br />
This following is all the code you may ever need in your main package. It creates a new application instance thus calling OnInit, and starts the main application loop, which is responsible for dispatching GUI events to windows. The loop will not terminate until there are top level windows.<br />
<br />
<pre style="white-space: pre-wrap; word-wrap: break-word;">package main;
<i><span style="color: #6aa84f;"># create the application object, this will call OnInit
# before the constructor returns.
</span></i>
my $app = MyApp->new;
<span style="color: #6aa84f;"><i># process GUI events from the application this function
# will not return until the last frame is closed
</i></span>
$app->MainLoop;</pre>
<pre style="white-space: pre-wrap; word-wrap: break-word;"></pre>
<br />
And finally here is how this looks on screen.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbETEOKF_LNAQGsTRf7CxdERi-UG4wA_MeN6frHuMq_lcX5rw_7_z4UsSssFXivK3L9P58yn-KBnMZEnSh2UuDb3mcr9OCf16UymvfG2myW7wvwMbhoaMgAXFMXE8ugw-eEvgyNEALp9s/s1600/helloworld.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbETEOKF_LNAQGsTRf7CxdERi-UG4wA_MeN6frHuMq_lcX5rw_7_z4UsSssFXivK3L9P58yn-KBnMZEnSh2UuDb3mcr9OCf16UymvfG2myW7wvwMbhoaMgAXFMXE8ugw-eEvgyNEALp9s/s1600/helloworld.png" /></a></div>
<br />
<br />
The full text of the script is contained in <a href="http://wxperl.sourceforge.net/examples/helloworld.pl.txt" target="_blank">helloworld.pl.txt</a> (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).<br />
<br />
<br />Unknownnoreply@blogger.com8