Perl Gtk3设置按钮大小

huangapple go评论70阅读模式
英文:

Perl Gtk3 set button size

问题

按钮占据了所有可用的垂直空间;如何获得适当的按钮大小?

使用严格;
使用警告;
使用utf8;
使用实验;

子主要(){
    使用Gtk3 '-init';
    使用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();

    子退出函数 {
        Gtk3->main_quit();
        返回假;
    }

    Gtk3->main();
}

主要();

Perl Gtk3设置按钮大小

英文:

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();

Perl Gtk3设置按钮大小

答案1

得分: 4

你可以将hbox放入一个垂直框中。这将使按钮保持正常高度。所以,不要直接将hbox放入窗口,你可以创建一个垂直框,将hbox放入垂直框中,然后将垂直框放入窗口中。以下是添加垂直框后你的代码会是什么样子的:

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);

    # 创建一个垂直框,将hbox放入其中
    my $vbox = Gtk3::Box->new("vertical", 5);
    $vbox->pack_start($hbox, FALSE, FALSE, 0);
    $vbox->set_homogeneous(TRUE);

    # 将垂直框放入窗口
    $window->add($vbox);
    $window->show_all();

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

    Gtk3->main();
}

希望这对你有帮助!如果你需要进一步的翻译,请告诉我。

英文:

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();
}

huangapple
  • 本文由 发表于 2023年5月11日 15:18:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定