bear2012 发表于 2012-1-18 12:24:48

新手   来学习学习

andrew020 发表于 2012-5-15 13:31:34

- -

_von 发表于 2012-5-30 16:55:25

学习学习

zhangzaidian 发表于 2012-5-31 13:39:14

改的楼上的

#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

static gint count_temp=0;

void add(GtkWidget *widget, gpointer data)
{
    char str;
    count_temp++;
    sprintf(str,"%d",count_temp);
    str='\\0';
    gtk_label_set_text(GTK_LABEL(data),str);
}

void sub(GtkWidget *widget, gpointer data)
{
    char str;
    count_temp--;
    sprintf(str,"%d",count_temp);
    str='\\0';
    gtk_label_set_text(GTK_LABEL(data),str);
}

gint delete_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
    gtk_main_quit();
    return FALSE;
}

int main (int argc, char *argv[])
{
    GtkWidget *win;
    GtkWidget *btn1,*btn2;
    GtkWidget *label;
    GtkWidget *box;
   
    gtk_init(&argc,&argv);

    win=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(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);
    label = gtk_label_new("a label");
    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),label,TRUE,FALSE,10);
    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),label);
    g_signal_connect(G_OBJECT(btn2),"clicked",G_CALLBACK(sub),label);

    gtk_widget_show_all(win);

    gtk_main();
    return 0;
}

a4150902 发表于 2012-6-21 11:06:19

ACTom 发表于 2012-6-21 17:36:03

回 zhangzaidian 的帖子

zhangzaidian:#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

....... (2012-05-31 13:39) images/back.gif

str='\\0';
这个可以不写的,sprintf会自动在后面加\\0的。。

minielephant 发表于 2012-6-29 09:45:12

??

潘田博客www.pan 发表于 2012-12-5 17:43:13

easior 发表于 2013-1-12 16:29:45

黄昏菩提 发表于 2013-6-21 08:58:19

页: 1 [2] 3
查看完整版本: 2、简单的GTK+事件绑定