英文:
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();
}
主要();
英文:
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();
答案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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论