创建在C按钮GTK阵列 [英] Creating array of buttons gtk in c
问题描述
我需要建立在C按钮的排列,我不知道我错过了什么,请帮助我。这里是我的数组:
的GtkWidget *键[5];
INT I;
为(ⅰ= 1; I&小于5;我+ +)
按钮[I] = gtk_button_new();
然后我创建按钮的其余部分...我使用按钮[I]
键,然后在最后我做到这一点我++;
这可能不是最好的方式,但我只是不知道,当我创建一个数组,我怎么通过按钮1,按钮2,等我发言的休息吗?
请任何帮助AP preciated。
附:我用C是新的,不要在我身上苛刻,TY :)
/ *创建带有标签按钮1的新按钮。 * /
按钮[I] = gtk_button_new_with_label(按钮1);/ *现在在点击按钮时,我们称之为回调功能
*用指针到按钮1为argiument * /
g_signal_connect(按钮[I]中,点击,
G_CALLBACK(回调),运行按钮1);/ *代替gtk_container_add,我们收拾这个按钮化为无形
*箱,已装入该窗口。 * /
gtk_box_pack_start(GTK_BOX(BOX1),按键[I],TRUE,TRUE,0);/ *一定要记住这一步,它告诉GTK我们preparation为
*这个按钮就完成了,现在可以显示出来。 * /
gtk_widget_show(按钮[]);
我++;
您for循环指数为1的指标变量(i)开始。但是在计算机的内存中按钮的排列,你用
声明的GtkWidget *按键[5];
始于索引0(I = 0)
因此,例如您的code应该是这个样子:
的GtkWidget *键[5];
//没有必要的,因为C99可以在里面申报(),例如的for(int i = 0;我小于5;我++)
INT I;
对于(I = 0; I&小于5;我+ +)
{
按钮[I] = gtk_button_new();
}
//做其他的东西
然后使用按钮只是访问您可以像一个普通的数组例如:
按钮[0] = gtk_button_new_with_label(按钮1);
您不需要我++内的循环,因为for循环会自动每次循环之后递增迭代器(变量i)(这是什么我++在为最终(i = 0; I< 5;我++)一样)
I need to create array of buttons in C. I am not sure what am I missing, please help me. Here is my array:
GtkWidget *button[5];
int i;
for ( i =1; i<5; i++)
button[i] = gtk_button_new();
Then I creating the rest of the buttons... I am using button [i]
and then at the end i do this i++;
This probably not the best way, but i am just not sure when I create the array, how do I pass button 1, button 2 and etc in the rest of my statements?
Please any help appreciated.
p.s. I am new in C, dont be harsh on me, ty:)
/* Creates a new button with the label "Button 1". */
button[i] = gtk_button_new_with_label ("Button 1");
/* Now when the button is clicked, we call the "callback" function
* with a pointer to "button 1" as its argiument */
g_signal_connect (button[i], "clicked",
G_CALLBACK (callback), "Run button 1");
/* Instead of gtk_container_add, we pack this button into the invisible
* box, which has been packed into the window. */
gtk_box_pack_start (GTK_BOX (box1), button[i], TRUE, TRUE, 0);
/* Always remember this step, this tells GTK that our preparation for
* this button is complete, and it can now be displayed. */
gtk_widget_show (button[i]);
i++;
your for loop starts with an index variable (i) of 1 however in the computer's memory the index to the array of buttons that you declared with
GtkWidget *button[5];
starts with an index of 0 (i = 0) so for example your code should look something like:
GtkWidget *button[5];
//not necessary since c99 can declare inside for() e.g for(int i = 0; i < 5; i++)
int i;
for(i = 0; i < 5; i++)
{
button[i] = gtk_button_new();
}
//do other stuff
then to use the buttons just access them like you would a regular array eg:
button[0] = gtk_button_new_with_label("button 1");
you do not need the i++ inside of the for loop because the for loop automatically increments the iterator (i variable) after every loop (this is what the i++ at the end of for(i = 0; i < 5; i++) does)
这篇关于创建在C按钮GTK阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!