如何在gtk上使用任何小部件的可见性?

时间:2016-03-25 20:03:28

标签: c gtk gtk3

我是GTK的新手...... 我想先隐藏标签,当我点击"点击"按钮它会显示标签。我使用gtk_widget_hide(标签)... bt它不工作。并将使用固定容器不要改变它。 这是我的代码==>

#include <gtk/gtk.h>

void destroy(GtkWidget* widget,gpointer *data){
    /*gtk_main_quit();*/
    g_print ("hide\n");

}

int main(int argc, char *argv[])
{
    GtkWidget *window,*button,*label,*fixed;

    gtk_init(&argc,&argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window),"Demo");
    gtk_widget_set_size_request(window,200,400);
    gtk_container_set_border_width(GTK_CONTAINER(window),10);

    fixed = gtk_fixed_new();

    button = gtk_button_new_with_mnemonic("Click");
    gtk_widget_set_size_request(button,20,20);

    label = gtk_label_new("BOOM!!");
    gtk_widget_hide(label);

    g_signal_connect_swapped(G_OBJECT(button),"clicked",G_CALLBACK(destroy),NULL);

    gtk_fixed_put(GTK_FIXED(fixed),button,0,0);
    gtk_fixed_put(GTK_FIXED(fixed),label,30,30);

    gtk_container_add(GTK_CONTAINER(window),fixed);


    gtk_widget_show_all(window);

    gtk_main();
    return 0;
}

1 个答案:

答案 0 :(得分:5)

gtk_widget_hide()结束时使用gtk_widget_show_all(window) 因为你已经在固定容器中使用了标签而固定包含在窗口中。然后你调用gtk_widget_show_all(window)这意味着显示窗口中的所有内容....这就是为什么必须像这样使用===&gt;

#include <gtk/gtk.h>

void destroy(GtkWidget* label,gpointer *data){
    /*gtk_main_quit();*/
    g_print ("hide\n");
    gtk_widget_show(label);

}

int main(int argc, char *argv[])
{
    GtkWidget *window,*button,*label,*fixed;

    gtk_init(&argc,&argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window),"Demo");
    gtk_widget_set_size_request(window,200,400);
    gtk_container_set_border_width(GTK_CONTAINER(window),10);

    fixed = gtk_fixed_new();

    button = gtk_button_new_with_mnemonic("Click");
    gtk_widget_set_size_request(button,20,20);

    label = gtk_label_new("BOOM!!");


    g_signal_connect_swapped(G_OBJECT(button),"clicked",G_CALLBACK(destroy),label);

    gtk_fixed_put(GTK_FIXED(fixed),button,0,0);
    gtk_fixed_put(GTK_FIXED(fixed),label,30,30);

    gtk_container_add(GTK_CONTAINER(window),fixed);


    gtk_widget_show_all(window);
    gtk_widget_hide(label);
    gtk_main();
    return 0;
}