0

我用 GTK 用 C 语言做了一个小程序,里面有一个窗口、一个盒子和一个桌子。桌子上涂有一些文件。我想用其他数据刷新表格,但我不知道怎么做。

此代码启动一个带有GtkTable. 该表填充了定时器句柄函数。第一次按 1,2,3 的顺序画三条线,最后一次按 3,2,1 的顺序画三条线。但表永远不会刷新。

#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;
}
4

1 回答 1

2

您的代码有几个问题:

  • 您没有将 vbox 添加到窗口中。这可以通过以下代码完成: gtk_container_add (GTK_CONTAINER (window), vboxgeneral);

  • 您将错误的参数(表格小部件)传递给您的 time_handler 函数。在该函数中,您创建了一个新表,丢失了您打包到 vbox 中的前一个表。您可以做的是销毁以前的表并使用新标签创建一个新表并将其打包到 vbox 中。

  • 在您的 main 函数中调用 time_handler(table) 是完全没有必要的。计时器已经为您做到了。

我制作的修改后的代码(按照您的要求执行的工作代码)如下:

#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;
}
于 2012-09-05T02:45:48.350 回答