gtk3 - 使用 Vala 编译后 GTK+ 小部件未显示在窗口中

标签 gtk3 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();,您的代码中的内容。

关于gtk3 - 使用 Vala 编译后 GTK+ 小部件未显示在窗口中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52458758/

相关文章:

c - 如何在按下鼠标按钮时打印连续消息(g_timeout_add)?

c GTK g_signal_connect 使用数据字段

cmake - 使用 cmake : `Undefined reference to ' g_thread_init'` 编译 vala

function - 我可以让一个函数在 Vala 中处理多个数据类型参数吗?

linux - 使用 Vala 列出文件夹内容

json - 使用 Vala 解析 JSON

c - 当应用程序随启动应用程序一起启动时,没有按钮单击声音

c - 如何在 Linux mint for windows 中编译 C GTK3+ 程序?

c++ - Gtk+ 删除 Gtk::Treeview 上第一列的填充/边距

vala - 需要 vala 中的 setter/getter