Search code examples
perlgtk3

Perl Gtk3 set button size


The button occupies all the available vertical space; how to get the proper button size?

use strict;
use warnings;
use utf8;
use experimentals;

sub main() {
    use Gtk3 '-init';
    use Glib qw/TRUE FALSE/;

    my $window = Gtk3::Window->new('toplevel');

    $window->set_title("Gtk3 Buttons");
    $window->set_position("center");
    $window->set_default_size(400, 200);
    $window->set_border_width(10);
    $window->signal_connect(delete_event => \&quit_function);

    my $quitButton = Gtk3::Button->new("Quit");
    $quitButton->signal_connect(clicked => \&quit_function);

    my $hbox = Gtk3::Box->new("horizontal", 5);
    $hbox->pack_start($quitButton, FALSE, FALSE, 0);
    $hbox->set_homogeneous(TRUE);

    $window->add($hbox);
    $window->show_all();

    sub quit_function {
        Gtk3->main_quit();
        return FALSE;
    }

    Gtk3->main();
}

main();

enter image description here


Solution

  • You can put the hbox inside a vertical box. This will allow the button to stay at a normal height. So instead of putting the hbox directly into the window, you can create a vertical box, put the hbox inside the vertical box, and then put the vertical box inside the window. Here is what your code would look like with a vertical box added in:

    use strict;
    use warnings;
    use utf8;
    use experimentals;
    
    sub main() {
        use Gtk3 '-init';
        use Glib qw/TRUE FALSE/;
    
        my $window = Gtk3::Window->new('toplevel');
    
        $window->set_title("Gtk3 Buttons");
        $window->set_position("center");
        $window->set_default_size(400, 200);
        $window->set_border_width(10);
        $window->signal_connect(delete_event => \&quit_function);
    
        my $quitButton = Gtk3::Button->new("Quit");
        $quitButton->signal_connect(clicked => \&quit_function);
    
        my $hbox = Gtk3::Box->new("horizontal", 5);
        $hbox->pack_start($quitButton, FALSE, FALSE, 0);
        $hbox->set_homogeneous(TRUE);
    
        # Create a vertical box, and put the hbox inside
        my $vbox = Gtk3::Box->new("vertical", 5);
        $vbox->pack_start($hbox, FALSE, FALSE, 0);
        $vbox->set_homogeneous(TRUE);
    
        # Put the vertical box into the window
        $window->add($vbox);
        $window->show_all();
    
        sub quit_function {
            Gtk3->main_quit();
            return FALSE;
        }
    
        Gtk3->main();
    }