我是靠谱客的博主 雪白云朵,最近开发中收集的这篇文章主要介绍GTK+学习笔记3——按钮构件,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

GTK+ 的按钮构件有很多种,常用的有如下四个:

GtkButton:普通按钮

GtkToggleButton:开关按钮

GtkCheckButton:复选按钮

GtkRadioButton:单选按钮

这四个构件的继承关系如下:



GtkButton:

GtkButton就是普通的按钮,带有多种信号,常用的有以下三种:

leave:鼠标离开按钮时触发的信号;

pressed:鼠标按下按钮时触发的信号;

clicked:鼠标松开时触发的信号。

下面这里例子会新建一个按钮,当触发不同信号时,会在终端打印相应的信息:

#include <gtk/gtk.h> void close_app(GtkWidget *window , gpointer data) { gtk_main_quit(); } void button_leave(GtkWidget *button,gpointer data) { printf("button leaven"); } void button_pressed(GtkWidget *button,gpointer data) { printf("button pressedn"); } void button_clicked(GtkWidget *button,gpointer data) { printf("button clickedn"); } int main(int argc,char *argv[]) { GtkWidget *window; GtkWidget *button; gtk_init(&argc,&argv); //新建一个窗口 window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window),"button demo"); gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER); gtk_window_set_default_size(GTK_WINDOW(window),200,200); g_signal_connect(GTK_OBJECT(window),"destroy",G_CALLBACK(close_app),NULL); //新建一个按钮 button = gtk_button_new_with_label("button"); //鼠标离开按钮时触发leave信号 g_signal_connect(GTK_OBJECT(button),"leave",G_CALLBACK(button_leave),NULL); //鼠标按下按钮时触发pressed信号 g_signal_connect(GTK_OBJECT(button),"pressed",G_CALLBACK(button_pressed),NULL); //鼠标松开是触发clicked信号 g_signal_connect(GTK_OBJECT(button),"clicked",G_CALLBACK(button_clicked),NULL); //将按钮放入窗口中 gtk_container_add(GTK_CONTAINER(window),button); gtk_widget_show_all(window); gtk_main(); return 0; }


GtkToggleButton:

开关按钮的特点是有两个状态:开和关。点击一次,按钮被按下(TRUE),再点击一次,按钮会弹起(FALSE)。当点击开关按钮时,会发出toggle信号。可以用如下函数获取开关按钮的状态:

gboolean gtk_toggle_button_get_active (GtkToggleButton *toggle_button);例子:
#include <gtk/gtk.h> void close_app(GtkWidget *window,gpointer data) { gtk_main_quit(); } void toggle_button_callback(GtkWidget *toggle,gpointer data) { if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle))) { //改变按钮的标签 gtk_button_set_label(GTK_BUTTON(toggle),"toggle button down"); } else { gtk_button_set_label(GTK_BUTTON(toggle),"toggle button up"); } } int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *toggle; gtk_init(&argc,&argv); //新建一个窗口 window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window),"toggle button demo"); gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER); gtk_window_set_default_size(GTK_WINDOW(window),200,200); g_signal_connect(GTK_OBJECT(window),"destroy",G_CALLBACK(close_app),NULL); //新建一个按钮 toggle = gtk_toggle_button_new_with_label("toggle button up"); g_signal_connect(GTK_OBJECT(toggle),"toggled",G_CALLBACK(toggle_button_callback),NULL); gtk_container_add(GTK_CONTAINER(window),toggle); gtk_widget_show_all(window); gtk_main(); return 0; }
GtkCheckButton:

多选按钮继承了开关按钮的特性,也是两种状态:选中或未选,与开关按钮使用同样的信号和函数获取状态。

例子:

#include <gtk/gtk.h> void close_app(GtkWidget *window, gpointer data) { gtk_main_quit(); } void check_button_callback(GtkWidget *check_button, gpointer data) { if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_button))) { printf("check button selectedn"); } else { printf("check button unselectedn"); } } int main(int argc,char *argv[]) { GtkWidget *window; GtkWidget *check_button; gtk_init(&argc,&argv); //新建一个窗口 window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window),"check button demo"); gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER); gtk_window_set_default_size(GTK_WINDOW(window),200,200); g_signal_connect(GTK_OBJECT(window),"destroy",G_CALLBACK(close_app),NULL); //新建一个多选按钮 check_button = gtk_check_button_new_with_label("check button 1"); g_signal_connect(GTK_OBJECT(check_button),"toggled",G_CALLBACK(check_button_callback),NULL); gtk_container_add(GTK_CONTAINER(window),check_button); gtk_widget_show_all(window); gtk_main(); return 0; }


GtkRadioButton:

单选按钮继承了多选按钮,同样可以使用toggle信号和gtk_toggle_button_get_active函数获取状态。与多选按钮不同,单选按钮需要分组,同组内的多个单选按钮只能选中一个。可以用如下函数创建一个单选按钮:

GtkWidget * gtk_radio_button_new (GSList *group);单选按钮必须要加入一个组:

void gtk_radio_button_set_group (GtkRadioButton *radio_button, GSList *group);例子:

#include <gtk/gtk.h> void close_app(GtkWidget *window,gpointer data) { gtk_main_quit(); } int main(int argc,char *argv[]) { GtkWidget *window; GSList *radio_group; GtkWidget *radio_button1; GtkWidget *radio_button2; GtkWidget *Vbox; gtk_init(&argc,&argv); //新建一个窗口 window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window),"radio button demo"); gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER); gtk_window_set_default_size(GTK_WINDOW(window),200,200); g_signal_connect(GTK_OBJECT(window),"destroy",G_CALLBACK(close_app),NULL); //新建两个单选按钮 radio_button1 = gtk_radio_button_new_with_label(NULL,"radio button 1"); radio_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio_button1)); radio_button2 = gtk_radio_button_new_with_label(radio_group,"radio button 2"); Vbox = gtk_vbox_new(FALSE,0); gtk_box_pack_start(GTK_BOX(Vbox),radio_button1,FALSE,FALSE,0); gtk_box_pack_start(GTK_BOX(Vbox),radio_button2,FALSE,FALSE,0); gtk_container_add(GTK_CONTAINER(window),Vbox); gtk_widget_show_all(window); gtk_main(); return 0; }

最后

以上就是雪白云朵为你收集整理的GTK+学习笔记3——按钮构件的全部内容,希望文章能够帮你解决GTK+学习笔记3——按钮构件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部