【问题标题】:GTK+ widgets not showing in window after compiled with Vala使用 Vala 编译后 GTK+ 小部件未显示在窗口中
【发布时间】:2018-09-22 16:45:38
【问题描述】:

我是非常大的 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);

}

【问题讨论】:

    标签: gtk3 vala


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多