Help language development. Donate to The Perl Foundation

GTK::Simple zef:finanalyst last updated on 2023-02-25

#!/usr/bin/env perl6

use v6;
use lib 'lib';
use GTK::Simple;
use GTK::Simple::App;

    Like every GTK::Simple application, we begin by
    creating a new C<GTK::Simple::App>.

my GTK::Simple::App $app .= new(title => "Toggle buttons");

    This time, we create a C<GTK::Simple::Label> to display a bit of
    info to the user and above and below that we create one
    C<GTK::Simple::CheckButton> and a C<GTK::Simple::ToggleButton>.

        my $check_button  = => "check me out!"),
        my $status_label  = => "the toggles are off and off"),
        my $toggle_button => "time to toggle!"),

    Since the window would end up terribly tiny otherwise, we set a
    quite generous inner border for the window

$app.border-width = 50;

    This sub will be called whenever we toggle either of the two Buttons.

sub update_label($b) {
    $status_label.text = "the toggles are " ~
        ($check_button, $toggle_button)>>{ <off on>[$_] }).join(" and ");

    Now all we need to do is to connect the C<update_label> sub to the
    C<toggled> supply of the buttons.

$check_button .toggled.tap: &update_label;
$toggle_button.toggled.tap: &update_label;

    Finally, we let the event loop run.