|
发表于 2011-12-7 16:11:44
|
显示全部楼层
#include <gtk/gtk.h>
static gint count_temp=0;
void add(GtkWidget *widget, gpointer data)
{
count_temp++;
g_print("%d\\n",count_temp);
}
void sub(GtkWidget *widget, gpointer data)
{
count_temp--;
g_print("%d\\n",count_temp);
}
gint delete_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
gtk_main_quit();
return FALSE;
}
int main (int argc, int *argv[])
{
GtkWidget *win;
GtkWidget *btn1,*btn2;
GtkWidget *box;
//初始化
gtk_init(&argc,&argv);
//建立窗体并初始各项参数
win=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(G_OBJECT(win),"My Second GTK Programme!");
g_signal_connect(G_OBJECT(win),"delete_event",G_CALLBACK(delete_event),NULL);
gtk_window_set_position(GTK_WINDOW(win),GTK_WIN_POS_CENTER);
gtk_container_border_width(G_OBJECT(win),10);
btn1=gtk_button_new_with_label("+");
btn2=gtk_button_new_with_label("-");
box=gtk_vbox_new(TRUE,0);
gtk_box_pack_start(GTK_BOX(box),btn1,TRUE,FALSE,10);
gtk_box_pack_start(GTK_BOX(box),btn2,TRUE,FALSE,10);
gtk_container_add(GTK_CONTAINER(win),box);
//四参数分别是:信号发送源对象,连接信号名称,
//捕获信号时所需调用的函数,传给此函数的参数。
g_signal_connect(G_OBJECT(btn1),"clicked",G_CALLBACK(add),NULL);
g_signal_connect(G_OBJECT(btn2),"clicked",G_CALLBACK(sub),NULL);
gtk_widget_show_all(win);
gtk_main();
return 0;
}
注:搞不定label,不知道在GTK里怎么把整形变量转为字符传给label。只能把结果显示在console模式下了。 |
|