我在C中用GTK做了一个小程序,窗口,盒子和桌子里有一个桌子。该表涂有一些文件。我想用其他数据刷新表格,但我不知道如何。
此代码启动一个GtkTable
的窗口。该表填充了计时器句柄功能。第一次按1,2,3顺序画三行,最后一次按顺序写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;
}
答案 0 :(得分:2)
您的代码有几个问题:
您没有将vbox添加到窗口中。这可以使用以下代码完成:
gtk_container_add (GTK_CONTAINER (window), vboxgeneral);
您将错误的参数(表格小部件)传递给time_handler函数。在该函数中,您创建了一个新表,该表丢失了您打包到vbox中的前一个表。您可以做的是销毁上一个表并使用新标签创建一个新表并将其打包到vbox中。
主函数中的调用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;
}