我用 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;
}