4. Pushbutton

Let's create a window, 400 x 200, with text, icon, white background and positioned at center. Recall that MainWindow is a widget. So if we insert another widget than we can use the same properties. The widget we want to insert now, is a Pushbutton, 100 x 50, with text, grey tint background color and positioned at center. Notice that use Prima qw(Application); is now extended with the modules Themes and Buttons.

The module Themes allows flat widgets, i.e. the apps become a modern design.

mainwindow
use Prima qw(Application Themes Buttons); # invoke the modules Application, Themes and Buttons

$mw = Prima::MainWindow-> create(
size => [400, 200],
text => 'Application with Pushbutton',
backColor => 0xFFFFFF,
growMode => gm::Center,
);

$mw-> insert(
Button =>
size => [100, 50], # notice the properties are the same as the properties of MainWindow
text => 'Exit',
backColor => 0xcccccc,
growMode => gm::Center,
);
run Prima;
The first thing you probably want is adding an action to the button. That's easy: assign a subroutine to the onClick property.
$mw-> insert( 
Button =>
size => [100, 50],
text => 'Exit',
backColor => 0xcccccc,
growMode => gm::Center,
onClick => sub { $::application-> close }
);
The Prima::Application class has only one special instance $::application. This application instance is necessary in case of widget and window, or event loop functionality. The application will be closed after clicking the button.


A shorter way of coding with the same result:

use Prima qw(Application Buttons);

Prima::MainWindow-> create(
    size => [400, 200],
    text => 'Application with Pushbutton',
    backColor => 0xFFFFFF,
    growMode => gm::Center, 
)-> insert( Button =>
    size => [100, 50],
    text => 'Exit', 
    backColor => 0xcccccc,
    growMode => gm::Center,
    onClick => sub { $::application-> close }
);

run Prima;