我想用按钮创建网格。当点击一个按钮时,我希望它改变颜色,根据按钮的当前状态,0或1被存储在一个数组中。用GTK按钮创建网格
现在我通过创建带有两个for循环(行和列)的按钮来完成此操作。 在for循环中;
/*Create an ID number for the button being created*/
btn_nr ++;
char btn_nr_str[3];
sprintf(btn_nr_str,"%d",btn_nr); //convert nr to string
/*Create button*/
button = gtk_button_new();
/* When the button is clicked, we call the "callback" function
* with a pointer to the ID */
gtk_signal_connect (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (callback)(gpointer) btn_nr_str);
/* Insert button into the table */
gtk_table_attach_defaults (GTK_TABLE(table), button, col, col+1, row, row+1);
gtk_widget_show (button);
该回调函数;
void callback(GtkWidget *widget, gpointer nr)
{
GdkColor buttonColor;
gdk_color_parse ("black", &buttonColor);
gtk_widget_modify_bg (GTK_WIDGET(widget), GTK_STATE_NORMAL, &buttonColor);
g_print ("Hello again - %s was pressed\n", (char *) nr);
}
这些按钮是按想要的方式创建的,点击时会变成黑色。 但是,所有按钮都会打印上次创建的按钮ID。
如何传递正确的ID?
您在'gtk_signal_connect'调用(缺少逗号)中有复制和粘贴问题。请在发布代码之前仔细检查,因为这会造成误解。 – ntd 2013-04-29 07:58:40