glade3教程(二)--简单绑定事件
今天的教程主要是简单事件的绑定。 首先是在上个教程所做的窗口上加上容器里的“固定的” 然后在窗口的左边放置两个按钮 一个名称改成add 标签改成+ 另一个名称改成sub 标签改成—,然后在按钮的右边放置一个标签将名称改成label 标签改成0 另存为addsub.glade 如图http://www.bailangcn.tk/content/uploadfile/201203/thum-10fb15c77258a991b0028080a64fb42d20120314145800.png 打开输入以下代码 并保存为2.c(具体请看注释)/*作者:bailangcn时间:2012/3/14作者主页:bailangcn.tk*/#include <gtk/gtk.h>gint num=0;
char buf;void add_label(GtkWidget *widget, gpointer label)
{
num++;
sprintf(buf,"%d",num);
/*这是gtk的一个函数,用来给Label设定文字*/
gtk_label_set_text(GTK_LABEL(label),buf);
}
void sub_label(GtkWidget *widget, gpointer label)
{
num--;
sprintf(buf,"%d",num);
/*这是gtk的一个函数,用来给Label设定文字*/
gtk_label_set_text(GTK_LABEL(label),buf);
}
int main(int argc, char *argv[])
{
GtkBuilder *builder; gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *add;
GtkWidget *sub;
GtkWidget *label;
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "addsub.glade", NULL); window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
add = GTK_WIDGET(gtk_builder_get_object(builder, "add"));
sub = GTK_WIDGET(gtk_builder_get_object(builder, "sub"));
label = GTK_WIDGET(gtk_builder_get_object(builder, "label")); g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(G_OBJECT(add),"clicked",G_CALLBACK(add_label),(gpointer)label);
g_signal_connect(G_OBJECT(sub),"clicked",G_CALLBACK(sub_label),(gpointer)label);
gtk_builder_connect_signals(builder, NULL);
// 释放builder的空间
g_object_unref(G_OBJECT(builder)); gtk_widget_show_all(window);
gtk_main();
return 0;
}
编译运行如下:(按下加按钮 右边的数字加一按下减数字减一)http://www.bailangcn.tk/content/uploadfile/201203/thum-09dd8c2662b96ce14928333f055c558020120314150023.png
本文始发于:http://www.bailangcn.tk/?post=11转载请注明
我的沙发啊- -
页:
[1]