首页 > 解决方案 > 为什么按钮没有展开以填满所有空间?

问题描述

我制作了一个小型测试 GtkD DUB 项目(以 GtkD 3.9.0 作为依赖项)来制作一个最小的可重现示例,说明我在一个更大的项目中遇到的问题。显然 Box 没有扩展中间的 Widget(在本例中为 Button)以占用所有可用空间。难道我做错了什么?

import gtk.MainWindow;
import gtk.Label;
import gtk.Button;
import gtk.Main;
import gtk.Box;

void main(string[] args) {
    Main.init(args);

    MainWindow window = new MainWindow("Pack test");
    Box box = new Box(Orientation.VERTICAL, 0);
    Label topLabel = new Label("Top");
    box.add(topLabel);
    box.packStart(topLabel, false, false, 0);
    Button bigButton = new Button("Big button");
    box.add(bigButton);
    box.packStart(bigButton, true, true, 0);
    Label bottomLabel = new Label("Bottom");
    box.add(bottomLabel);
    box.packStart(bottomLabel, false, false, 0);
    window.add(box);
    window.setDefaultSize(800, 600);
    window.showAll();

    Main.run();
}

当我在 Glade 中制作相同的结构并进行预览时,它显示按钮已展开以填充所有可用空间,所以要么我做错了,要么 GtkD 中存在错误......

标签: dgtkd

解决方案


Mike Wey(GtkD 作者)解决了这个线程中的谜团 - https://forum.gtkd.org/groups/GtkD/thread/2069/ ...显然我同时调用了 add() 和 packStart() 却不知道packStart() 实际上添加了小部件,所以看起来像添加小部件两次(一次使用 add(),然后使用 packStart())会使事情变得混乱。

因此删除了 add() 调用的代码按预期工作:

import gtk.MainWindow;
import gtk.Label;
import gtk.Button;
import gtk.Main;
import gtk.Box;

void main(string[] args) {
    Main.init(args);

    MainWindow window = new MainWindow("Pack test");
    Box box = new Box(Orientation.VERTICAL, 0);
    Label topLabel = new Label("Top");
    //box.add(topLabel);
    box.packStart(topLabel, false, false, 0);
    Button bigButton = new Button("Big button");
    //box.add(bigButton);
    box.packStart(bigButton, true, true, 0);
    Label bottomLabel = new Label("Bottom");
    //box.add(bottomLabel);
    box.packStart(bottomLabel, false, false, 0);
    window.add(box);
    window.setDefaultSize(800, 600);
    window.showAll();

    Main.run();
}

推荐阅读