GTK编程基础----按钮构件

创建按钮有好几种方法。你可以用 gtk_button_new_with_label() 或 gtk_button_new_with_mnemonic() 来创建一个带标签的按钮,用 gtk_button_new_from_stock() 来从一个原料(stock)项创建一个包含图像和文本的按钮,或者用 gtk_button_new() 创建一个空白按钮。接着你可以决定把一个标签或位图(pixmap)组装到这个新创建的按钮里。要这样做,创建一个新的盒,然后用常见的 gtk_box_pack_start() 把你的对象组装到盒里,再gtk_container_add() 把盒组装到按钮里。

下面是演示用函数gtk_button_new()去创建一个带图像和标签的按钮控件。

 
/*File:Normal_Button.c
 *Date:2013-12-03
 *Author:sjin
 *Mail:[email protected]
 */
#include <stdio.h>
#include <gtk/gtk.h>
#include <stdlib.h>

/*创建一个横向盒,它包含一个图像和一个标签,并非你会这个盒子*/
GtkWidget *xpm_label_box(gchar *xpm_filename,gchar *label_text)
{
    GtkWidget *box;
    GtkWidget *label;
    GtkWidget *image;

    /*为图像和标签创建盒*/
    box = gtk_hbox_new(FALSE,0);
    gtk_container_set_border_width(GTK_CONTAINER(box),2);

    /*创建一个图像*/
    image = gtk_image_new_from_file(xpm_filename);

    /*为按钮创建一个标签*/
    label = gtk_label_new(label_text);

    /*图像和按钮组装到盒子里*/
    gtk_box_pack_start(GTK_BOX(box),image,FALSE,FALSE,3);
    gtk_box_pack_start(GTK_BOX(box),label,FALSE,FALSE,3);
    
    gtk_widget_show(image);
    gtk_widget_show(label);

    return box;
}


/*回调函数
 *传到这个函数的数据将呗打印到标准输出
 * */
void callback(GtkWidget *widget, gpointer data)
{
    g_print("Hello again -%s was pressed\n",(char *) data);
}

/*退出回调函数*/
gint delete_event(GtkWidget *widget,GdkEvent *event,gpointer data)
{
    gtk_main_quit();
    return FALSE;
}


int main(int argc,char *argv[])
{
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *box;

    /*初始化*/
    gtk_init(&argc,&argv);

    /*创建窗口*/
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    /*这个一般必要有delete_event信号到主窗口*/
    g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(delete_event),NULL);
    
    /*设置窗口宽度*/
    gtk_container_set_border_width(GTK_CONTAINER(window),80);

    /*设置窗口标题*/
    gtk_window_set_title(GTK_WINDOW(window),"普通按钮练习");

    button = gtk_button_new();
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(callback),(gpointer)"cool button");

    box = xpm_label_box("info.xpm","cool button");
    gtk_widget_show(box);
    gtk_container_add(GTK_CONTAINER(button),box);

    gtk_widget_show(button);
    gtk_container_add(GTK_CONTAINER(window),button);

    gtk_widget_show(window);

    gtk_main();
    return 0;
}
运行程序显示下面的图像:

GTK编程基础----按钮构件_第1张图片

按钮构件有如下信号:
• pressed --- 当鼠标键在按钮构件里按下时发出
• released --- 当鼠标键在按钮构件里释放时发出
• clicked --- 当鼠标键在按钮构件里按下并接着在按钮构件里释放时发出
• enter --- 当鼠标光标进入按钮构件时发出
• leave --- 当鼠标光标离开按钮构件时发出

开关、复选按钮

  GTK编程基础----按钮构件_第2张图片
/*File:Toggle_button.c
 *Date:2013-12-07
 *Author:sjin
 *Mail:[email protected]
 */
#include <stdio.h>
#include <gtk/gtk.h>
#include <stdlib.h>

/*开关按钮:
 *  由一般按钮派生而来,只是开关按钮只有两个状态,通过点击
 *  可以切换。
 *  开关按钮是复选按钮和单选按钮的基础,所以单选按钮和复选按钮继承了
 *  许多开关按钮的函数调用
 */

    GtkWidget *button1;
    GtkWidget *button2;

/*回调函数
 *传到这个函数的数据将呗打印到标准输出
 * */
void Toggle_Button_callback(GtkWidget *widget, gpointer data)
{
    if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))){
        g_print(" 按钮被按下\n");
    }else{
        g_print("按钮是弹起的\n",(char *) data);

    }
}

void callback(GtkWidget *widget, gpointer data)
{
    if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button1))){
        g_print(" 数学被选中\n");
    }else{
        g_print("nothing\n");
    }
    if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button2))){
        g_print(" 语文被选中\n");
    }else{
        g_print("nothing\n");
    }
}

/*退出回调函数*/
gint delete_event(GtkWidget *widget,GdkEvent *event,gpointer data)
{
    gtk_main_quit();
    return FALSE;
}


int main(int argc,char *argv[])
{
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *box;

    /*初始化*/
    gtk_init(&argc,&argv);

    /*创建窗口*/
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    /*这个一般必要有delete_event信号到主窗口*/
    g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(delete_event),NULL);
    
    /*设置窗口宽度*/
    gtk_container_set_border_width(GTK_CONTAINER(window),20);

    /*设置窗口标题*/
    gtk_window_set_title(GTK_WINDOW(window),"开关按钮练习");
    
    /*创建一个组装盒子*/
    box = gtk_vbox_new(FALSE,0);

    /*设置一个开关按钮*/
    button = gtk_toggle_button_new_with_label("打开");
    /*设置按钮的状态,默认为弹起 FALSE .*/
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),FALSE);
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(Toggle_Button_callback),(gpointer)"cool button");
    gtk_box_pack_start(GTK_BOX(box),button,TRUE,TRUE,0);
    gtk_widget_show(button);

    /***************设置复选按钮****************/
    button1 = gtk_check_button_new_with_label("数学");
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button1),TRUE);
    gtk_box_pack_start(GTK_BOX(box),button1,TRUE,TRUE,0);
    gtk_widget_show(button1);

    button2 = gtk_check_button_new_with_label("语文");
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button2),TRUE);
    gtk_box_pack_start(GTK_BOX(box),button2,TRUE,TRUE,0);
    gtk_widget_show(button2);

    button = gtk_button_new_with_label("OK");
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),TRUE);
    gtk_box_pack_start(GTK_BOX(box),button,TRUE,TRUE,0);
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(callback),NULL);
    gtk_widget_show(button);

    gtk_container_add(GTK_CONTAINER(window),box);

    gtk_widget_show(box);
    gtk_widget_show(window);
    

    gtk_main();
    return 0;
}


 单选按钮 

GTK编程基础----按钮构件_第3张图片

 
/*File:Radio_Button.c
 *Date:2013-12-03
 *Author:sjin
 *Mail:[email protected]
 */
#include <stdio.h>
#include <gtk/gtk.h>
#include <stdlib.h>

/*创建单选按钮,点击close 回输出那个单选按钮被点击
 * 单选按钮是分组的,一个组中只能有一个对象被选中、按下
 *当应用程序中,要从多个选项中选中一个时用到
 */

enum Subject{
    SHUXVE,
    YUWEN,
    YINGYU
};
    GtkWidget *button1;//语文
    GtkWidget *button2;//数学
    GtkWidget *button3;//英语

void Radio_Button_callback(GtkWidget *widget,gpointer data)
{
    if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button1))){
        g_print("语文被点击\n");
    }
    if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button2))){
        g_print("数学被点击\n");
    }
    if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button3))){
        g_print("英语被点击\n");
    }

}

/*回调函数
 *传到这个函数的数据将呗打印到标准输出
 * */
void callback(GtkWidget *widget, gpointer data)
{
    g_print("Hello again -%s was pressed\n",(char *) data);
}

/*退出回调函数*/
gint delete_event(GtkWidget *widget,GdkEvent *event,gpointer data)
{
    gtk_main_quit();
    return FALSE;
}


int main(int argc,char *argv[])
{
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *box1;
    GtkWidget *box2;
    GtkWidget *separator;
    GSList *group;

    /*初始化*/
    gtk_init(&argc,&argv);

    /*创建窗口*/
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    /*这个一般必要有delete_event信号到主窗口*/
    g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(delete_event),NULL);
    
    /*设置窗口宽度*/
    gtk_container_set_border_width(GTK_CONTAINER(window),0);

    /*设置窗口标题*/
    gtk_window_set_title(GTK_WINDOW(window),"单选按钮练习");

    box1 = gtk_vbox_new(FALSE,0);
    gtk_container_add(GTK_CONTAINER(window),box1);
    gtk_widget_show(box1);

    box2 = gtk_vbox_new(FALSE,10);
    gtk_container_set_border_width(GTK_CONTAINER(box2),10);
    gtk_box_pack_start(GTK_BOX(box1),box2,TRUE,TRUE,0);
    gtk_widget_show(box2);

    button1 = gtk_radio_button_new_with_label(NULL,"语文");
    gtk_box_pack_start(GTK_BOX(box2),button1,TRUE,TRUE,0);
    gtk_widget_show(button1);

    group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(button1));
    button2 = gtk_radio_button_new_with_label(group,"数学");
    /*默认是选择这个*/
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button2),TRUE);
    gtk_box_pack_start(GTK_BOX(box2),button2,TRUE,TRUE,0);
    gtk_widget_show(button2);
    
    button3 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(button2),"英语");
    gtk_box_pack_start(GTK_BOX(box2),button3,TRUE,TRUE,0);
    gtk_widget_show(button3);

    /*分割线*/
    separator = gtk_hseparator_new();
    gtk_box_pack_start(GTK_BOX(box1),separator,FALSE,TRUE,0);
    gtk_widget_show(separator);
    
    box2 = gtk_vbox_new(FALSE,10);
    gtk_container_set_border_width(GTK_CONTAINER(box2),10);
    gtk_box_pack_start(GTK_BOX(box1),box2,FALSE,TRUE,0);
    gtk_widget_show(box2);
    
    /*退出按钮*/
    button = gtk_button_new_with_label("close");
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(Radio_Button_callback),(gpointer)"cool button");
    gtk_box_pack_start(GTK_BOX(box2),button,TRUE,TRUE,0);
    GTK_WIDGET_SET_FLAGS(button,GTK_CAN_DEFAULT);
    gtk_widget_grab_default(button);
    gtk_widget_show(button);
    gtk_widget_show(window);
    

    gtk_main();
    return 0;
}

你可能感兴趣的:(单选按钮,开关按钮,复选按钮,普通按钮)