我已经填充了一个包含 4 列的表。第一列是填充有循环的复选框。事实上,所有列都填充了一个循环。第二列是文本条目 entry[x][y],其中 x 是列,y 是行。所以这就是我想要做的。当您选中第 1 行 (checkbox[y]) 中的复选框时,我想更改第 2 列第 1 行 (entry[x][y]) 中输入框的文本颜色。我知道答案很简单,但它让我难以理解。想法?
问问题
388 次
1 回答
0
嗯,这是我的例子。可能更简单但有效。
#include <gtk/gtk.h>
#include <stdio.h>
#define TABLE 4
#define C_RED "red"
#define C_BLACK "black"
typedef struct _Widgets Widgets;
struct _Widgets
{
GtkWidget *checkbox;
GtkWidget *entry[TABLE-1];
};
static void change_color (GtkWidget *widget, gpointer data)
{
int i;
GdkColor color;
Widgets *_widget = (Widgets*) data;
gchar *cc = NULL;
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
cc = C_RED;
else
cc = C_BLACK;
gdk_color_parse (cc, &color);
for (i = 0; i < TABLE; i++)
gtk_widget_modify_fg (_widget->entry[i], GTK_STATE_NORMAL, &color);
}
int main (int argc, char **argv)
{
int x,z;
int pos = 0;
GtkWidget *window = NULL;
GtkWidget *table = NULL;
Widgets widgets[TABLE];
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW(window), "Feel Gtk Table");
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
table = gtk_table_new (TABLE, TABLE, FALSE);
gtk_container_add (GTK_CONTAINER (window), table);
for (x = 0; x < TABLE; x++)
{
widgets[x].checkbox = gtk_check_button_new ();
gtk_table_attach_defaults (GTK_TABLE(table), widgets[x].checkbox, 0, 1, x, x+1);
g_signal_connect (G_OBJECT (widgets[x].checkbox), "toggled", G_CALLBACK (change_color), &widgets[x]);
for (z = 0; z < TABLE-1; z++)
{
widgets[x].entry[z] = gtk_entry_new();
gtk_table_attach_defaults (GTK_TABLE(table), widgets[x].entry[z], z+1, z+2, x, x+1);
}
}
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
于 2012-05-29T22:41:09.903 回答