Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8514519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:53:34+00:00 2026-06-11T04:53:34+00:00

I’ve done a little program in C with GTK with a window, a box

  • 0

I’ve done a little program in C with GTK with a window, a box and a table inside the box. The table is painted with some files. I’d like to refresh the table with other data but I don’t know how.

This code launches a window with a GtkTable. This table is filled in the timer handle function. First time draw three lines in order 1,2,3 and last time write three lines in order 3,2,1. But the table never refreshes.

#include <stdlib.h>
#include <gtk/gtk.h>


static gboolean time_handler(GtkWidget *table);
gboolean launched;

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *vboxgeneral;
    gchar *title;
    GtkWidget *table;

    gtk_init(&argc, &argv);

    /* WINDOW */
    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);

    /* ADDING GTKTABLE TO VBOX */
    table = gtk_table_new(1,1,FALSE);
    gtk_box_pack_start(GTK_BOX(vboxgeneral), table, FALSE, FALSE, 0);

    /* SHOW ALL */
    gtk_widget_show_all(window);

    /* ACTIONS */
    g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

    /* START TIMER */
    g_timeout_add(10000, (GSourceFunc) time_handler, (gpointer) window);
    time_handler(table);

    gtk_main();


    return 0;
}



static gboolean time_handler(GtkWidget *table)
{
    GtkWidget *widget;
    gboolean result = FALSE;

    /* ADDING TABLE DEFINITION */

    if(!launched)
    {
        table = gtk_table_new(3,1,FALSE);

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



        result = FALSE;
        g_print("Timer executed last time!\n");
    }

     gtk_widget_draw(table, NULL);


    return result;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:53:36+00:00Added an answer on June 11, 2026 at 4:53 am

    Your code has several issues:

    • You didn’t add your vbox to your window. That can be done with the following code:
      gtk_container_add (GTK_CONTAINER (window), vboxgeneral);

    • 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.

    • The call time_handler(table) made in your main function is completely unnecesary. The timer already does that for you.

    The revised code I made (a working code which does what you asked) as follows:

    #include <stdlib.h>
    #include <gtk/gtk.h>
    
    static gboolean time_handler(GtkWidget *table);
    gboolean launched;
    
    int main( int argc, char *argv[])
    {
        GtkWidget *window;
        GtkWidget *vboxgeneral;
        gchar *title;
    
        gtk_init(&argc, &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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.