使用 Vala 编译后 GTK+ 小部件未显示在 window 中

GTK+ widgets not showing in window after compiled with Vala

我是一个非常大的 Vala 新手。我制作了一个 cusButton 函数,它在某种程度上用于重构其他按钮并减少代码行。 编译后 运行 我有一个空屏幕。(没有显示) 这可能是一个愚蠢的错误。你们能指出我的错误吗?

   Gtk.Button cusButton(string label,Gtk.Box grid ){
       var button  = new Gtk.Button.with_label(label);    
       button.show();
       grid.pack_start(button,true,true,0);
       return button;   
}


public class window:Gtk.ApplicationWindow{
    internal window(MyApplication app){
    Object (application:app,title:"TCalc");
    this.set_default_size(640,1136);


    this.window_position = Gtk.WindowPosition.CENTER;
    var parent = new Gtk.Box(Gtk.Orientation.VERTICAL,0);
    //Row 1
    //int left, int top, int width , int height 
    var row1 = new Gtk.Box(Gtk.Orientation.HORIZONTAL,0);
    var clear_button = cusButton("AC",row1);
    var ac_button = cusButton("<-",row1);
    var perc_button = cusButton("%",row1);
    var div_button = cusButton("/",row1);

    //Row 2
    var row2 = new Gtk.Box(Gtk.Orientation.HORIZONTAL,0);
    var sev_button = cusButton("7",row2);
    var eight_button = cusButton("8",row2);
    var nine_button = cusButton("9",row2);
    var multi_button = cusButton("X",row2);
    //A few more rows..

    parent.pack_start(row1,false,false,1);
    parent.pack_start(row2,false,false,1);

    this.add(parent);
    parent.show();

}
}

public class MyApplication : Gtk.Application { 
protected override void activate(){
    new window (this).show();
}
internal MyApplication () {
    Object (application_id: "org.example.MyApplication");
}

}
public static int main(string[] args) {
   return new MyApplication().run(args);

}

尝试使用 show_all () 而不是 show ()。这样可以节省您在所有小部件上调用 show 的时间。所以改变:

protected override void activate(){
    new window (this).show();
}

protected override void activate(){
    new window (this).show_all();
}

您还可以删除对 show () 的其他调用,例如button.show();,您的代码中有。