Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code has several issues:</p> <ul> <li><p>You didn't add your vbox to your window. That can be done with the following code: <code>gtk_container_add (GTK_CONTAINER (window), vboxgeneral);</code></p></li> <li><p>You were passing the wrong parameter (the table widget) to your time_handler function. In that function, you created a new table losing the previous one which you packed into a vbox. What you can do is destroy the previous table and create a new one with the new labels and pack it into the vbox. </p></li> <li><p>The call time_handler(table) made in your main function is completely unnecesary. The timer already does that for you.</p></li> </ul> <p>The revised code I made (a working code which does what you asked) as follows:</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;gtk/gtk.h&gt; static gboolean time_handler(GtkWidget *table); gboolean launched; int main( int argc, char *argv[]) { GtkWidget *window; GtkWidget *vboxgeneral; gchar *title; gtk_init(&amp;argc, &amp;argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); title = "Test GtkTable"; gtk_window_set_title(GTK_WINDOW(window), title); gtk_window_set_default_size(GTK_WINDOW(window), 200, 100); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); /* ADDING GTKHBOX TO MAIN WINDOW */ vboxgeneral = gtk_vbox_new(FALSE, 0); /* This step was missing! */ gtk_container_add (GTK_CONTAINER (window), vboxgeneral); gtk_widget_show_all(window); g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL); /* START TIMER */ /* refresh in 1 second, change to the desired amount */ g_timeout_add(1000, (GSourceFunc) time_handler, (gpointer) vboxgeneral); gtk_main(); return 0; } static gboolean time_handler(GtkWidget *vbox) { GtkWidget *widget; static GtkWidget *table = NULL; gboolean result = FALSE; /* if you want only the new labels to appear and not the previous ones you can: */ /* 1. destroy the table and create a new one. This will be done */ /* 2. hide all previous labels in the table and create and show the new ones */ if (table != NULL) { /* destroy previous table packed into vbox */ gtk_widget_destroy(table); } table = gtk_table_new(3,1,FALSE); /* create new table for the new labels */ if(!launched) { widget = gtk_label_new("File 1"); gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER); gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 0, 1); widget = gtk_label_new("File 2"); gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER); gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 1, 2); widget = gtk_label_new("File 3"); gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER); gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 2, 3); launched = TRUE; result = TRUE; g_print("Timer executed first time!\n"); } else { /* table = gtk_table_new(3,1,FALSE); */ widget = gtk_label_new("File 3"); gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER); gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 0, 1); widget = gtk_label_new("File 2"); gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER); gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 1, 2); widget = gtk_label_new("File 1"); gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER); gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 2, 3); launched = FALSE; result = FALSE; g_print("Timer executed last time!\n"); } /* ADD THE NEWLY CREATED TABLE TO THE VBOX */ gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0); /* VERY IMPORTANT: SHOW ALL THE NEW LABELS PACKED INTO THE TABLE */ gtk_widget_show_all(table); return result; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload