Search code examples
gtk3vala

GTK+ widgets not showing in window after compiled with Vala


I am avery big Vala Newbie. I made a cusButton function which is somewhat there for refactoring the other buttons and reduce lines of code. After compiling and running I have an Empty Screen.( Nothing is shown) Its probably a stupid mistake. Can you guys point me to my mistake?

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

}

Solution

  • Try using show_all () instead of show (). This saves you calling show on all the widgets. So change:

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

    to

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

    You can also then remove the other calls to show (), e.g. button.show();, that you have in your code.